home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / as / dist / vax.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-05-19  |  92.9 KB  |  3,193 lines

  1. /* vax.c - vax-specific -
  2.    Copyright (C) 1987 Free Software Foundation, Inc.
  3.  
  4. This file is part of GAS, the GNU Assembler.
  5.  
  6. GAS is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GAS is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GAS; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* JF I moved almost all the vax specific stuff into this one file 'cuz RMS
  21.    seems to think its a good idea.  I hope I managed to get all the VAX-isms */
  22.  
  23.  
  24. #include "as.h"
  25. #include "read.h"
  26. #include "flonum.h"
  27. #include "vax-inst.h"
  28. #include "md.h"
  29. #include "obstack.h"        /* For FRAG_APPEND_1_CHAR macro in "frags.h" */
  30. #include "frags.h"
  31. #include "struc-symbol.h"
  32. #include "expr.h"
  33. #include "symbols.h"
  34.  
  35. /* This is the number to put at the beginning of the a.out file */
  36. long omagic = OMAGIC;
  37.  
  38. /* These chars start a comment anywhere in a source file (except inside
  39.    another comment */
  40. char comment_chars[] = "#";
  41.  
  42. /* These chars only start a comment at the beginning of a line. */
  43. /* Note that for the VAX the are the same ac comment_chars above. */
  44. char line_comment_chars[] = "#";
  45.  
  46. /* Chars that can be used to separate mant from exp in floating point nums */
  47. char EXP_CHARS[] = "eE";
  48.  
  49. /* Chars that mean this number is a floating point constant */
  50. /* as in 0f123.456 */
  51. /* or    0H1.234E-12 (see exp chars above) */
  52. char FLT_CHARS[] = "dDfFgGhH";
  53.  
  54. /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
  55.    changed in read.c .  Ideally it shouldn't have to know about it at all,
  56.    but nothing is ideal around here.
  57.  */
  58.  
  59. static expressionS        /* Hold details of an operand expression */
  60. exp_of_operand [ VIT_MAX_OPERANDS ];
  61.  
  62. static struct vit
  63. v;                /* A vax instruction after decoding. */
  64.  
  65. LITTLENUM_TYPE big_operand_bits [ VIT_MAX_OPERANDS ] [ SIZE_OF_LARGE_NUMBER ];
  66.                 /* Hold details of big operands. */
  67. FLONUM_TYPE float_operand [ VIT_MAX_OPERANDS ];
  68.                 /* Above is made to point into */
  69.                 /* big_operand_bits by md_begin(). */
  70.  
  71. /*
  72.  * For VAX, relative addresses of "just the right length" are easy.
  73.  * The branch displacement is always the last operand, even in
  74.  * synthetic instructions.
  75.  * For VAX, we encode the relax_substateTs (in e.g. fr_substate) as:
  76.  *
  77.  *            4       3       2       1       0         bit number
  78.  *    ---/ /--+-------+-------+-------+-------+-------+
  79.  *        |     what state ?    |  how long ?    |
  80.  *    ---/ /--+-------+-------+-------+-------+-------+
  81.  *
  82.  * The "how long" bits are 00=byte, 01=word, 10=long.
  83.  * This is a Un*x convention.
  84.  * Not all lengths are legit for a given value of (what state).
  85.  * The "how long" refers merely to the displacement length.
  86.  * The address usually has some constant bytes in it as well.
  87.  *
  88.  
  89. groups for VAX address relaxing.
  90.  
  91. 1.    "foo" pc-relative.
  92.     length of byte, word, long
  93.  
  94. 2a.    J<cond> where <cond> is a simple flag test.
  95.     length of byte, word, long.
  96.     VAX opcodes are:    (Hex)
  97.         bneq/bnequ    12
  98.         beql/beqlu    13
  99.         bgtr        14
  100.         bleq        15
  101.         bgeq        18
  102.         blss        19
  103.         bgtru        1a
  104.         blequ        1b
  105.         bvc        1c
  106.         bvs        1d
  107.         bgequ/bcc    1e
  108.         blssu/bcs    1f
  109.     Always, you complement 0th bit to reverse condition.
  110.     Always, 1-byte opcode, then 1-byte displacement.
  111.  
  112. 2b.    J<cond> where cond tests a memory bit.
  113.     length of byte, word, long.
  114.     Vax opcodes are:    (Hex)
  115.         bbs        e0
  116.         bbc        e1
  117.         bbss        e2
  118.         bbcs        e3
  119.         bbsc        e4
  120.         bbcc        e5
  121.         bbssi        e6
  122.         bbcci        e7
  123.     Always, you complement 0th bit to reverse condition.
  124.     Always, 1-byte opcde, longword-address, byte-address, 1-byte-displacement
  125.  
  126. 2c.    J<cond> where cond tests low-order memory bit
  127.     length of byte,word,long.
  128.     Vax opcodes are:    (Hex)
  129.         blbs        e8
  130.         blbc        e9
  131.     Always, you complement 0th bit to reverse condition.
  132.     Always, 1-byte opcode, longword-address, 1-byte displacement.
  133.  
  134. 3.    Jbs/Jbr.
  135.     length of byte,word,long.
  136.     Vax opcodes are:    (Hex)
  137.         bsbb        10
  138.         brb        11
  139.     These are like (2) but there is no condition to reverse.
  140.     Always, 1 byte opcode, then displacement/absolute.
  141.  
  142. 4a.    JacbX
  143.     length of word, long.
  144.     Vax opcodes are:    (Hex)
  145.         acbw        3d
  146.         acbf        4f
  147.         acbd        6f
  148.         abcb        9d
  149.         acbl        f1
  150.         acbg          4ffd
  151.         acbh          6ffd
  152.     Always, we cannot reverse the sense of the branch; we have a word
  153.     displacement.
  154.     The double-byte op-codes don't hurt: we never want to modify the
  155.     opcode, so we don't care how many bytes are between the opcode and
  156.     the operand.
  157.  
  158. 4b.    JXobXXX
  159.     length of long, long, byte.
  160.     Vax opcodes are:    (Hex)
  161.         aoblss        f2
  162.         aobleq        f3
  163.         sobgeq        f4
  164.         sobgtr        f5
  165.     Always, we cannot reverse the sense of the branch; we have a byte
  166.     displacement.
  167.  
  168. The only time we need to modify the opcode is for class 2 instructions.
  169. After relax() we may complement the lowest order bit of such instruction
  170. to reverse sense of branch.
  171.  
  172. For class 2 instructions, we store context of "where is the opcode literal".
  173. We can change an opcode's lowest order bit without breaking anything else.
  174.  
  175. We sometimes store context in the operand literal. This way we can figure out
  176. after relax() what the original addressing mode was.
  177. */
  178.  
  179.                 /* These displacements are relative to */
  180.                 /* the start address of the displacement. */
  181.                 /* The first letter is Byte, Word. */
  182.                 /* 2nd letter is Forward, Backward. */
  183. #define BF (1+ 127)
  184. #define BB (1+-128)
  185. #define WF (2+ 32767)
  186. #define WB (2+-32768)
  187.                 /* Dont need LF, LB because they always */
  188.                 /* reach. [They are coded as 0.] */
  189.  
  190.  
  191. #define C(a,b) ENCODE_RELAX(a,b)
  192.                 /* This macro has no side-effects. */
  193. #define ENCODE_RELAX(what,length) (((what) << 2) + (length))
  194.  
  195. relax_typeS
  196. md_relax_table[] = {
  197. {       1,       1,    0,    0    },    /* error sentinel   0,0    */
  198. {       1,       1,    0,    0    },    /* unused        0,1    */
  199. {       1,       1,    0,    0    },    /* unused        0,2    */
  200. {       1,       1,    0,    0    },    /* unused        0,3    */
  201. {    BF+1,    BB+1,    2,    C(1,1)    },    /* B^"foo"        1,0 */
  202. {    WF+1,    WB+1,    3,    C(1,2)    },    /* W^"foo"        1,1 */
  203. {       0,       0,    5,    0    },    /* L^"foo"        1,2 */
  204. {       1,       1,    0,    0    },    /* unused        1,3 */
  205. {    BF  ,    BB  ,    1,    C(2,1)    },    /* b<cond> B^"foo"  2,0 */
  206. {    WF+2,    WB+2,    4,    C(2,2)    },    /* br.+? brw X        2,1 */
  207. {       0,       0,    7,    0    },    /* br.+? jmp X        2,2 */
  208. {       1,       1,    0,    0    },    /* unused        2,3 */
  209. {    BF  ,    BB  ,    1,    C(3,1)    },    /* brb B^foo        3,0 */
  210. {    WF  ,    WB  ,    2,    C(3,2)    },    /* brw W^foo        3,1 */
  211. {       0,      0,   5,    0    },    /* Jmp L^foo        3,2 */
  212. {       1,       1,    0,    0    },    /* unused        3,3 */
  213. {       1,       1,    0,    0    },    /* unused        4,0 */
  214. {    WF  ,    WB  ,   2,    C(4,2)    },    /* acb_ ^Wfoo        4,1 */
  215. {       0,       0,  10,    0    },    /* acb_,br,jmp L^foo4,2 */
  216. {       1,       1,    0,    0    },    /* unused        4,3 */
  217. {    BF  ,    BB  ,    1,    C(5,1)    },    /* Xob___,,foo      5,0 */
  218. {    WF+4,    WB+4,    6,    C(5,2)    },    /* Xob.+2,brb.+3,brw5,1 */
  219. {       0,       0,    9,    0    },    /* Xob.+2,brb.+6,jmp5,2 */
  220.   };
  221.  
  222. #undef C
  223. #undef BF
  224. #undef BB
  225. #undef WF
  226. #undef WB
  227.  
  228. void    float_cons();
  229.  
  230. pseudo_typeS md_pseudo_table[] = {
  231.     { "dfloat",    float_cons,    'd' },
  232.     { "ffloat",    float_cons,    'f' },
  233.     { "gfloat",    float_cons,    'g' },
  234.     { "hfloat",    float_cons,    'h' },
  235.     { 0 }
  236. };
  237.  
  238. #define STATE_PC_RELATIVE        (1)
  239. #define STATE_CONDITIONAL_BRANCH    (2)
  240. #define STATE_ALWAYS_BRANCH        (3) /* includes BSB... */
  241. #define STATE_COMPLEX_BRANCH            (4)
  242. #define STATE_COMPLEX_HOP        (5)
  243.  
  244. #define STATE_BYTE            (0)
  245. #define STATE_WORD            (1)
  246. #define STATE_LONG            (2)
  247. #define STATE_UNDF            (3) /* Symbol undefined in pass1 */
  248.  
  249.  
  250. #define min(a, b)    ((a) < (b) ? (a) : (b))
  251.  
  252.  
  253. void
  254. md_begin()
  255. {
  256.   char * vip_begin();
  257.   char * errtxt;
  258.   FLONUM_TYPE *    fP;
  259.   int    i;
  260.  
  261.   if ( * (errtxt = vip_begin(TRUE, "$", "*", "`")) )
  262.     {
  263.       as_fatal( "VIP_BEGIN error:%s", errtxt );
  264.     }
  265.  
  266.   for (i = 0, fP = float_operand;
  267.        fP < float_operand + VIT_MAX_OPERANDS;
  268.        i++, fP ++)
  269.     {
  270.       fP -> low  = & big_operand_bits [i] [0];
  271.       fP -> high = & big_operand_bits [i] [SIZE_OF_LARGE_NUMBER - 1];
  272.     }
  273. }
  274.  
  275. void
  276. md_end()
  277. {
  278.   vip_end();
  279. }
  280.  
  281. void                /* Knows about order of bytes in address. */
  282. md_number_to_chars (con, value, nbytes)
  283.      char    con [];    /* Return 'nbytes' of chars here. */
  284.      long int    value;        /* The value of the bits. */
  285.      int    nbytes;        /* Number of bytes in the output. */
  286. {
  287.   int n;
  288.   long v;
  289.  
  290.   n=nbytes;
  291.   v=value;
  292.   while (nbytes--)
  293.     {
  294.       *con++ = value;        /* Lint wants & MASK_CHAR. */
  295.       value >>= BITS_PER_CHAR;
  296.     }
  297.     /* XXX line number probably botched for this warning message. */
  298.     if (value != 0 && value != -1)
  299.       as_warn ("Displacement (%ld) long for instruction field length (%d).",v,n);
  300. }
  301.  
  302. void                /* Knows about order of bytes in address. */
  303. md_number_to_imm (con, value, nbytes)
  304.      char    con [];    /* Return 'nbytes' of chars here. */
  305.      long int    value;        /* The value of the bits. */
  306.      int    nbytes;        /* Number of bytes in the output. */
  307. {
  308.   int n;
  309.   long v;
  310.  
  311.   n=nbytes;
  312.   v=value;
  313.   while (nbytes--)
  314.     {
  315.       *con++ = value;        /* Lint wants & MASK_CHAR. */
  316.       value >>= BITS_PER_CHAR;
  317.     }
  318.     /* XXX line number probably botched for this warning message. */
  319.     if (value != 0 && value != -1)
  320.       as_warn ("Displacement (%ld) too long for instruction field length (%d).",v,n);
  321. }
  322.  
  323. void                /* Knows about order of bytes in address. */
  324. md_number_to_disp (con, value, nbytes)
  325.      char    con [];    /* Return 'nbytes' of chars here. */
  326.      long int    value;        /* The value of the bits. */
  327.      int    nbytes;        /* Number of bytes in the output. */
  328. {
  329.   abort();
  330.   while (nbytes--)
  331.     {
  332.       *con++ = value;        /* Lint wants & MASK_CHAR. */
  333.       value >>= BITS_PER_CHAR;
  334.     }
  335.     /* XXX line number probably botched for this warning message. */
  336.     if (value != 0 && value != -1)
  337.       as_warn ("Displacement too long for instruction field length.");
  338. }
  339.  
  340. void                /* Knows about order of bytes in address. */
  341. md_number_to_field (con, value, nbytes)
  342.      char    con [];    /* Return 'nbytes' of chars here. */
  343.      long int    value;        /* The value of the bits. */
  344.      int    nbytes;        /* Number of bytes in the output. */
  345. {
  346.   abort();
  347.   while (nbytes--)
  348.     {
  349.       *con++ = value;        /* Lint wants & MASK_CHAR. */
  350.       value >>= BITS_PER_CHAR;
  351.     }
  352.     /* XXX line number probably botched for this warning message. */
  353.     if (value != 0 && value != -1)
  354.       as_warn ("Displacement too long for instruction field length.");
  355. }
  356.  
  357. long int            /* Knows about the byte order in a word. */
  358. md_chars_to_number (con, nbytes)
  359. unsigned     char    con[];    /* Low order byte 1st. */
  360.      int    nbytes;        /* Number of bytes in the input. */
  361. {
  362.   long int    retval;
  363.   for (retval=0, con+=nbytes-1; nbytes--; con--)
  364.     {
  365.       retval <<= BITS_PER_CHAR;
  366.       retval |= *con;
  367.     }
  368.   return retval;
  369. }
  370.  
  371. /* vax:md_assemble() emit frags for 1 instruction */
  372.  
  373. void
  374. md_assemble (instruction_string)
  375.      char * instruction_string;    /* A string: assemble 1 instruction. */
  376. {
  377.            char *    p;
  378.   register struct vop *    operandP; /* An operand. Scans all operands. */
  379.            char *    save_input_line_pointer;
  380.            char        c_save;    /* What used to live after an expression. */
  381.            struct frag* fragP;    /* Fragment of code we just made. */
  382.   register int        goofed;    /* TRUE: instruction_string bad for all passes. */
  383.   register struct vop * end_operandP; /* -> slot just after last operand */
  384.                 /* Limit of the for (each operand). */
  385.   register expressionS *expP;    /* -> expression values for this operand */
  386.  
  387.                 /* These refer to an instruction operand expression. */
  388.        segT        to_seg;    /* Target segment of the address.     */
  389.   register valueT    this_add_number;
  390.   register struct symbol * this_add_symbol; /* +ve (minuend) symbol. */
  391.   register struct symbol * this_subtract_symbol; /* -ve(subtrahend) symbol. */
  392.  
  393.        long int    opcode_as_number; /* As a number. */
  394.        char *    opcode_as_chars; /* Least significant byte 1st. */
  395.                 /* As an array of characters. */
  396.        char *    opcode_low_byteP; /* Least significant byte 1st */
  397.        struct details * detP; /* The details of an ADxxx frag. */
  398.        int        length;    /* length (bytes) meant by vop_short. */
  399.        int        at;    /* 0, or 1 if '@' is in addressing mode. */
  400.        int        nbytes;    /* From vop_nbytes: vax_operand_width (in bytes) */
  401.        FLONUM_TYPE *    floatP;
  402.        char *    vip();
  403.        LITTLENUM_TYPE    literal_float [8];
  404.                 /* Big enough for any floating point literal. */
  405.  
  406.   if ( * ( p = vip (&v, instruction_string) ) )
  407.     {
  408.       as_fatal("vax_assemble\"%s\" in=\"%s\"", p, instruction_string);
  409.     }
  410.   /*
  411.    * Now we try to find as many as_warn()s as we can. If we do any as_warn()s
  412.    * then goofed=TRUE. Notice that we don't make any frags yet.
  413.    * Should goofed be TRUE, then this instruction will wedge in any pass,
  414.    * and we can safely flush it, without causing interpass symbol phase
  415.    * errors. That is, without changing label values in different passes.
  416.    */
  417.   if (   goofed = ( * v . vit_error )   )
  418.     {
  419.       as_warn("Ignoring statement due to \"%s\"", v . vit_error);
  420.     }
  421.   /*
  422.    * We need to use expression() and friends, which require us to diddle
  423.    * input_line_pointer. So we save it and restore it later.
  424.    */
  425.   save_input_line_pointer = input_line_pointer;
  426.   for (operandP = v.vit_operand,
  427.        expP = exp_of_operand,
  428.        floatP = float_operand,
  429.        end_operandP = v.vit_operand + v.vit_operands;
  430.  
  431.        operandP < end_operandP;
  432.  
  433.        operandP++,
  434.        expP++,
  435.        floatP ++
  436.        )            /* for each operand */
  437.     {
  438.       if (   * ( operandP -> vop_error ) )
  439.     {
  440.       as_warn("Ignoring statement because \"%s\"", ( operandP -> vop_error ) );
  441.       goofed = TRUE;
  442.     }
  443.       else
  444.     {/* statement has no syntax goofs: lets sniff the expression */
  445.       int    can_be_short;    /* TRUE if a bignum can be reduced to a short literal. */
  446.  
  447.       input_line_pointer = operandP -> vop_expr_begin;
  448.       c_save = operandP -> vop_expr_end [1];
  449.       operandP -> vop_expr_end [1] = '\0';
  450.       /* If to_seg == SEG_PASS1, expression() will have set need_pass_2 = TRUE. */
  451.       switch (to_seg = expression( expP ))
  452.         {
  453.         case SEG_NONE:
  454.           /* for BSD4.2 compatibility, missing expression is absolute 0 */
  455.           to_seg = expP -> X_seg = SEG_ABSOLUTE;
  456.           expP -> X_add_number = 0;
  457.           /* for SEG_ABSOLUTE, we shouldnt need to set X_subtract_symbol, X_add_symbol to any particular value. */
  458.           /* But, we will program defensively. Since this situation occurs */
  459.           /* rarely so it costs us little to do, and stops Dean */
  460.           /* worrying about the origin of random bits in expressionS's. */
  461.           expP -> X_add_symbol    = NULL;
  462.           expP -> X_subtract_symbol    = NULL;
  463.         case SEG_TEXT:
  464.         case SEG_DATA:
  465.         case SEG_BSS:
  466.         case SEG_ABSOLUTE:
  467.         case SEG_UNKNOWN:
  468.           break;
  469.  
  470.         case SEG_DIFFERENCE:
  471.           /*
  472.            * Major bug. We can't handle the case of a
  473.            * SEG_DIFFERENCE expression in a VIT_OPCODE_SYNTHETIC
  474.            * variable-length instruction.
  475.            * We don't have a frag type that is smart enough to
  476.            * relax a SEG_DIFFERENCE, and so we just force all
  477.            * SEG_DIFFERENCEs to behave like SEG_PASS1s.
  478.            * Clearly, if there is a demand we can invent a new or
  479.            * modified frag type and then coding up a frag for this
  480.            * case will be easy. SEG_DIFFERENCE was invented for the
  481.            * .words after a CASE opcode, and was never intended for
  482.            * instruction operands.
  483.            */
  484.           need_pass_2 = TRUE;
  485.           break;
  486.  
  487.         case SEG_BIG:
  488.                 /* Preserve the bits. */
  489.           if (expP -> X_add_number > 0)
  490.         {
  491.           bignum_copy (generic_bignum, expP -> X_add_number,
  492.                    floatP -> low, SIZE_OF_LARGE_NUMBER);
  493.         }
  494.           else
  495.         {
  496.           know( expP -> X_add_number  < 0 );
  497.           flonum_copy (& generic_floating_point_number,
  498.                    floatP);
  499.           if (index ("s i", operandP -> vop_short))
  500.             {        /* Could possibly become S^# */
  501.               flonum_gen2vax (- expP -> X_add_number, floatP, literal_float);
  502.               switch ( - expP -> X_add_number)
  503.             {
  504.             case 'f':
  505.               can_be_short =
  506.                 (literal_float [0] & 0xFC0F) == 0x4000
  507.                   && literal_float [1] == 0;
  508.               break;
  509.  
  510.             case 'd':
  511.               can_be_short =
  512.                 (literal_float [0] & 0xFC0F) == 0x4000
  513.                   && literal_float[1] == 0
  514.                 && literal_float [2] == 0
  515.                   && literal_float [3] == 0;
  516.               break;
  517.  
  518.             case 'g':
  519.               can_be_short =
  520.                 (literal_float [0] & 0xFF81) == 0x4000
  521.                   && literal_float[1] == 0
  522.                 && literal_float [2] == 0
  523.                   && literal_float [3] == 0;
  524.               break;
  525.  
  526.             case 'h':
  527.               can_be_short =
  528.                 (literal_float [0] & 0xFFF8) == 0x4000
  529.                   && (literal_float [1] & 0xE000) == 0
  530.                 && literal_float [2] == 0
  531.                   && literal_float [3] == 0
  532.                     && literal_float [4] == 0
  533.                       && literal_float [5] == 0
  534.                     && literal_float [6] == 0
  535.                       && literal_float [7] == 0;
  536.               break;
  537.  
  538.             default:
  539.               BAD_CASE( - expP -> X_add_number );
  540.               break;
  541.             }    /* switch (float type) */
  542.             }        /* if (could want to become S^#...) */
  543.         }        /* bignum or flonum ? */
  544.  
  545.           if (   operandP -> vop_short == 's'
  546.           || operandP -> vop_short == 'i'
  547.           || (   operandP -> vop_short == ' '
  548.               && operandP -> vop_reg == 0xF
  549.               && (operandP -> vop_mode & 0xE) == 0x8))
  550.         {
  551.           /* Saw a '#'. */
  552.           if (operandP -> vop_short == ' ')
  553.             {        /* We must chose S^ or I^. */
  554.               if (expP -> X_add_number > 0)
  555.             {    /* Bignum: Short literal impossible. */
  556.               operandP -> vop_short = 'i';
  557.               operandP -> vop_mode = 8;
  558.               operandP -> vop_reg = 0xF; /* VAX PC. */
  559.             }
  560.               else
  561.             {    /* Flonum: Try to do it. */
  562.               if (can_be_short)
  563.                 {
  564.                   operandP -> vop_short = 's';
  565.                   operandP -> vop_mode = 0;
  566.                   operandP -> vop_ndx = -1;
  567.                   operandP -> vop_reg = -1;
  568.                   /* JF hope this is the right thing */
  569.                   expP->X_seg=SEG_ABSOLUTE;
  570.                 }
  571.               else
  572.                 {
  573.                   operandP -> vop_short = 'i';
  574.                   operandP -> vop_mode = 8;
  575.                   operandP -> vop_reg = 0xF; /* VAX PC */
  576.                 }
  577.             }    /* bignum or flonum ? */
  578.             }    /*  if #, but no S^ or I^ seen. */
  579.           /* No more ' ' case: either 's' or 'i'. */
  580.           if (operandP -> vop_short == 's')
  581.             {
  582.               /* Wants to be a short literal. */
  583.               if (expP -> X_add_number > 0)
  584.             {
  585.               as_warn( "Bignum not permitted in short literal. Immediate mode assumed." );
  586.               operandP -> vop_short = 'i';
  587.               operandP -> vop_mode = 8;
  588.               operandP -> vop_reg = 0xF; /* VAX PC. */
  589.             }
  590.               else
  591.             {
  592.               if ( ! can_be_short)
  593.                 {
  594.                   as_warn( "Can't do flonum short literal: immediate mode used." );
  595.                   operandP -> vop_short = 'i';
  596.                   operandP -> vop_mode = 8;
  597.                   operandP -> vop_reg = 0xF; /* VAX PC. */
  598.                 }
  599.               else
  600.                 {    /* Encode short literal now. */
  601.                   register int    temp;
  602.  
  603.                   switch ( - expP -> X_add_number )
  604.                 {
  605.                 case 'f':
  606.                 case 'd':
  607.                   temp = literal_float [0] >> 4;
  608.                   break;
  609.  
  610.                 case 'g':
  611.                   temp = literal_float [0] >> 1;
  612.                   break;
  613.  
  614.                 case 'h':
  615.                   temp = ((literal_float [0] << 3) & 070)
  616.                     | ((literal_float [1] >> 13) & 07);
  617.                   break;
  618.  
  619.                 default:
  620.                   BAD_CASE( - expP -> X_add_number );
  621.                   break;
  622.                 }
  623.  
  624.                   floatP -> low [0] = temp & 077;
  625.                   floatP -> low [1] = 0;
  626.                 }    /* if can be short literal float */
  627.             }    /* flonum or bignum ? */
  628.             }
  629.           else
  630.             {        /* I^# seen: set it up if float. */
  631.               if (expP -> X_add_number < 0)
  632.             {
  633.               bcopy( literal_float, floatP -> low, sizeof(literal_float));
  634.             }
  635.             }        /* if S^# seen. */
  636.         }
  637.           else
  638.         {
  639.           as_warn( "A bignum/flonum may not be a displacement: 0x%x used",
  640.               expP -> X_add_number = 0x80000000);
  641.           /* Chosen so luser gets the most offset bits to patch later. */
  642.         }
  643.           expP -> X_add_number = floatP -> low [0]
  644.         | ((LITTLENUM_MASK & (floatP -> low [1])) << LITTLENUM_NUMBER_OF_BITS);
  645. /*
  646.  * For the SEG_BIG case we have:
  647.  * If vop_short == 's' then a short floating literal is in the
  648.  *    lowest 6 bits of floatP -> low [0], which is
  649.  *    big_operand_bits [---] [0].
  650.  * If vop_short == 'i' then the appropriate number of elements
  651.  *    of big_operand_bits [---] [...] are set up with the correct
  652.  *    bits.
  653.  * Also, just in case width is byte word or long, we copy the lowest
  654.  * 32 bits of the number to X_add_number.
  655.  */
  656.           break;
  657.  
  658.         default:
  659.           BAD_CASE( to_seg );
  660.           break;
  661.         }
  662.       if ( input_line_pointer != operandP -> vop_expr_end + 1 )
  663.         {
  664.           as_warn("Junk at end of expression \"%s\"", input_line_pointer);
  665.           goofed = TRUE;
  666.         }
  667.       operandP -> vop_expr_end [1] = c_save;
  668.     }
  669.     }                /* for(each operand) */
  670.   input_line_pointer = save_input_line_pointer;
  671.  
  672.   if ( ! need_pass_2 && ! goofed )
  673.     {
  674.       /* We saw no errors in any operands - try to make frag(s) */
  675.       int    is_undefined;    /* True if operand expression's */
  676.                 /* segment not known yet. */
  677.       int    length_code;
  678.  
  679.       /* Emit op-code. */
  680.       /* Remember where it is, in case we want to modify the op-code later. */
  681.       opcode_low_byteP = frag_more (v . vit_opcode_nbytes);
  682.       bcopy (v . vit_opcode, opcode_low_byteP, v . vit_opcode_nbytes);
  683.       opcode_as_number = md_chars_to_number (opcode_as_chars = v . vit_opcode, 4);
  684.       for (operandP = v.vit_operand,
  685.        expP = exp_of_operand,
  686.        floatP = float_operand,
  687.        end_operandP = v.vit_operand + v.vit_operands;
  688.  
  689.        operandP < end_operandP;
  690.  
  691.        operandP++,
  692.        floatP ++,
  693.        expP++
  694.        )            /* for each operand */
  695.     {
  696.       if ( operandP -> vop_ndx >= 0 )
  697.         {
  698.           /* indexed addressing byte */
  699.           /* Legality of indexed mode already checked: it is OK */
  700.           FRAG_APPEND_1_CHAR( 0x40 + operandP -> vop_ndx );
  701.         }            /* if(vop_ndx>=0) */
  702.  
  703.       /* Here to make main operand frag(s). */
  704.       this_add_number      = expP -> X_add_number;
  705.       this_add_symbol      = expP -> X_add_symbol;
  706.       this_subtract_symbol = expP -> X_subtract_symbol;
  707.       to_seg               = expP -> X_seg;
  708.       is_undefined = (to_seg == SEG_UNKNOWN);
  709.       know(   to_seg == SEG_UNKNOWN        \
  710.            || to_seg == SEG_ABSOLUTE    \
  711.            || to_seg == SEG_DATA        \
  712.            || to_seg == SEG_TEXT        \
  713.            || to_seg == SEG_BSS        \
  714.            || to_seg == SEG_BIG        \
  715.            );
  716.       at = operandP -> vop_mode & 1;
  717.       length = operandP->vop_short=='b' ? 1 : operandP->vop_short=='w' ? 2 : operandP->vop_short=='l' ? 4 : 0;
  718.       nbytes = operandP -> vop_nbytes;
  719.       if ( operandP -> vop_access == 'b' )
  720.         {
  721.           if (to_seg == now_seg || is_undefined)
  722.         {        /* If is_undefined, then it might BECOME now_seg. */
  723.           if ( nbytes )
  724.             {
  725.               p = frag_more (nbytes);
  726.               fix_new (frag_now, p - frag_now -> fr_literal, nbytes,
  727.                    this_add_symbol, 0, this_add_number, 1);
  728.             }
  729.           else
  730.             {        /* to_seg==now_seg || to_seg == SEG_UNKNOWN */
  731.                 /* nbytes==0 */
  732.               length_code = is_undefined ? STATE_UNDF : STATE_BYTE;
  733.               if ( opcode_as_number & VIT_OPCODE_SPECIAL )
  734.             {
  735.               if ( operandP -> vop_width == VAX_WIDTH_UNCONDITIONAL_JUMP )
  736.                 {
  737.                   /* br or jsb */
  738.                   frag_var (rs_machine_dependent, 5, 1,
  739.                        ENCODE_RELAX( STATE_ALWAYS_BRANCH, length_code ),
  740.                     this_add_symbol, this_add_number,
  741.                     opcode_low_byteP);
  742.                 }
  743.               else
  744.                 {
  745.                   if(operandP -> vop_width == VAX_WIDTH_WORD_JUMP)
  746.                 {
  747.                   length_code=STATE_WORD;    /* JF: There is no state_byte for this one! */
  748.                   frag_var (rs_machine_dependent, 10, 2,
  749.                         ENCODE_RELAX( STATE_COMPLEX_BRANCH, length_code ),
  750.                         this_add_symbol, this_add_number,
  751.                         opcode_low_byteP);
  752.                 }
  753.                   else
  754.                 {
  755.                   know( operandP -> vop_width == VAX_WIDTH_BYTE_JUMP );
  756.                   frag_var (rs_machine_dependent, 9, 1,
  757.                         ENCODE_RELAX( STATE_COMPLEX_HOP, length_code),
  758.                         this_add_symbol, this_add_number,
  759.                         opcode_low_byteP);
  760.                 }
  761.                 }
  762.             }
  763.               else
  764.             {
  765.               know( operandP -> vop_width == VAX_WIDTH_CONDITIONAL_JUMP );
  766.               frag_var (rs_machine_dependent, 7, 1,
  767.                    ENCODE_RELAX( STATE_CONDITIONAL_BRANCH, length_code ),
  768.                     this_add_symbol, this_add_number,
  769.                     opcode_low_byteP);
  770.             }
  771.             }
  772.         }
  773.           else
  774.         {        /* to_seg != now_seg && to_seg != SEG_UNKNOWN */
  775. /*
  776.  * --- SEG FLOAT MAY APPEAR HERE ----
  777.  */
  778.           if ( to_seg == SEG_ABSOLUTE )
  779.             {
  780.               if ( nbytes )
  781.             {
  782.               know( ! (opcode_as_number & VIT_OPCODE_SYNTHETIC) );
  783.               p = frag_more (nbytes);
  784.               /* Conventional relocation. */
  785.               fix_new (frag_now, p - frag_now -> fr_literal,
  786.                    nbytes, & abs_symbol, 0, this_add_number, 1);
  787.             }
  788.               else
  789.             {
  790.               know( opcode_as_number & VIT_OPCODE_SYNTHETIC );
  791.               if ( opcode_as_number & VIT_OPCODE_SPECIAL )
  792.                 {
  793.                   if ( operandP -> vop_width == VAX_WIDTH_UNCONDITIONAL_JUMP )
  794.                 {
  795.                   /* br or jsb */
  796.                   * opcode_low_byteP = opcode_as_chars[0] + VAX_WIDEN_LONG;
  797.                   know( opcode_as_chars [1] == 0 );
  798.                   p = frag_more (5);
  799.                   p [0] = VAX_ABSOLUTE_MODE; /* @#... */
  800.                   md_number_to_chars (p + 1, this_add_number, 4);
  801.                   /* Now (eg) JMP @#foo or JSB @#foo. */
  802.                 }
  803.                   else
  804.                 {
  805.                   if(operandP -> vop_width == VAX_WIDTH_WORD_JUMP)
  806.                     {
  807.                       p = frag_more (10);
  808.                       p[0] = 2;
  809.                       p[1] = 0;
  810.                       p[2] = VAX_BRB;
  811.                       p[3] = 6;
  812.                       p[4] = VAX_JMP;
  813.                       p[5] = VAX_ABSOLUTE_MODE; /* @#... */
  814.                       md_number_to_chars (p+6, this_add_number, 4);
  815.                       /*
  816.                        * Now (eg)    ACBx    1f
  817.                        *        BRB    2f
  818.                        *    1:    JMP    @#foo
  819.                        *    2:
  820.                        */
  821.                     }
  822.                   else
  823.                     {
  824.                       know( operandP -> vop_width == VAX_WIDTH_BYTE_JUMP );
  825.                       p = frag_more (9);
  826.                       p [0] = 2;
  827.                       p [1] = VAX_BRB;
  828.                       p [2] = 6;
  829.                       p [3] = VAX_JMP;
  830.                       p [4] = VAX_PC_RELATIVE_MODE + 1;    /* @#... */
  831.                       md_number_to_chars (p+5, this_add_number, 4);
  832.                       /*
  833.                        * Now (eg)    xOBxxx    1f
  834.                        *        BRB    2f
  835.                        *    1:    JMP    @#foo
  836.                        *    2:
  837.                        */
  838.                     }
  839.                 }
  840.                 }
  841.               else
  842.                 {
  843.                   /* b<cond> */
  844.                   * opcode_low_byteP ^= 1; /* To reverse the condition in a VAX branch, complement the lowest order bit. */
  845.                   p = frag_more (7);
  846.                   p[0] = 6;
  847.                   p[1] = VAX_JMP;
  848.                   p[2] = VAX_ABSOLUTE_MODE; /* @#... */
  849.                   md_number_to_chars( p+3, this_add_number, 4);
  850.                   /*
  851.                    * Now (eg)    BLEQ    1f
  852.                    *        JMP    @#foo
  853.                    *    1:
  854.                    */
  855.                 }
  856.             }
  857.             }
  858.           else
  859.             {        /* to_seg != now_seg && to_seg != SEG_UNKNOWN && to_Seg != SEG_ABSOLUTE */
  860.               if ( nbytes > 0)
  861.             {
  862.               /* Pc-relative. Conventional relocation. */
  863.               know( ! (opcode_as_number & VIT_OPCODE_SYNTHETIC) );
  864.               p = frag_more (nbytes);
  865.               fix_new (frag_now, p - frag_now -> fr_literal,
  866.                    nbytes, & abs_symbol, 0, this_add_number, 1);
  867.             }
  868.               else
  869.             {
  870.               know( opcode_as_number & VIT_OPCODE_SYNTHETIC );
  871.               if ( opcode_as_number & VIT_OPCODE_SPECIAL )
  872.                 {
  873.                   if ( operandP -> vop_width == VAX_WIDTH_UNCONDITIONAL_JUMP )
  874.                 {
  875.                   /* br or jsb */
  876.                   know( opcode_as_chars [1] == 0 );
  877.                   * opcode_low_byteP = opcode_as_chars[0] + VAX_WIDEN_LONG;
  878.                   p = frag_more (5);
  879.                   p [0] = VAX_PC_RELATIVE_MODE;
  880.                   fix_new (frag_now,
  881.                        p+1 - frag_now -> fr_literal, 4,
  882.                        this_add_symbol, 0,
  883.                        this_add_number, 1);
  884.                   /* Now eg JMP foo or JSB foo. */
  885.                 }
  886.                   else
  887.                 {
  888.                   if(operandP -> vop_width == VAX_WIDTH_WORD_JUMP)
  889.                     {
  890.                       p = frag_more (10);
  891.                       p[0] = 0;
  892.                       p[1] = 2;
  893.                       p[2] = VAX_BRB;
  894.                       p[3] = 6;
  895.                       p[4] = VAX_JMP;
  896.                       p[5] = VAX_PC_RELATIVE_MODE;
  897.                       fix_new (frag_now,
  898.                            p+6 - frag_now -> fr_literal, 4,
  899.                            this_add_symbol, 0,
  900.                            this_add_number, 1);
  901.                       /*
  902.                        * Now (eg)    ACBx    1f
  903.                        *        BRB    2f
  904.                        *    1:    JMP    foo
  905.                        *    2:
  906.                        */
  907.                     }
  908.                   else
  909.                     {
  910.                       know( operandP -> vop_width == VAX_WIDTH_BYTE_JUMP);
  911.                       p = frag_more (10);
  912.                       p [0] = 2;
  913.                       p [1] = VAX_BRB;
  914.                       p [2] = 6;
  915.                       p [3] = VAX_JMP;
  916.                       p [4] = VAX_PC_RELATIVE_MODE;
  917.                       fix_new (frag_now,
  918.                            p+5 - frag_now -> fr_literal,
  919.                            4, this_add_symbol, 0,
  920.                            this_add_number, 1);
  921.                       /*
  922.                        * Now (eg)    xOBxxx    1f
  923.                        *        BRB    2f
  924.                        *    1:    JMP    foo
  925.                        *    2:
  926.                        */
  927.                     }
  928.                 }
  929.                 }
  930.               else
  931.                 {
  932.                   know( operandP -> vop_width == VAX_WIDTH_CONDITIONAL_JUMP );
  933.                   * opcode_low_byteP ^= 1; /* Reverse branch condition. */
  934.                   p = frag_more (7);
  935.                   p[0] = 6;
  936.                   p[1] = VAX_JMP;
  937.                   p[2] = VAX_PC_RELATIVE_MODE;
  938.                   fix_new (frag_now, p+3 - frag_now -> fr_literal,
  939.                        4, this_add_symbol, 0,
  940.                        this_add_number, 1);
  941.                 }
  942.             }
  943.             }
  944.         }
  945.         }
  946.       else
  947.         {
  948.           know( operandP -> vop_access != 'b' ); /* So it is ordinary operand. */
  949.           know( operandP -> vop_access != ' ' ); /* ' ' target-independent: elsewhere. */
  950.           know( operandP -> vop_access=='a' || operandP -> vop_access=='m' || operandP -> vop_access=='r' || operandP -> vop_access=='v' || operandP -> vop_access=='w' );
  951.           if ( operandP -> vop_short == 's' )
  952.         {
  953.           if ( to_seg == SEG_ABSOLUTE )
  954.             {
  955.               if ( this_add_number <0 || this_add_number >= 64 )
  956.             {
  957.               as_warn("Short literal overflow(%d.), immediate mode assumed.",this_add_number);
  958.               operandP -> vop_short = 'i';
  959.               operandP -> vop_mode = 8;
  960.               operandP -> vop_reg = 0xF;
  961.             }
  962.             }
  963.           else
  964.             {
  965.               as_warn("Forced short literal to immediate mode. now_seg=%s to_seg=%s", seg_name[(int)now_seg], seg_name[(int)to_seg]);
  966.               operandP -> vop_short = 'i';
  967.               operandP -> vop_mode = 8;
  968.               operandP -> vop_reg = 0xF;
  969.             }
  970.         }
  971.           if ( operandP->vop_reg>=0 && (operandP->vop_mode<8 || (operandP->vop_reg!=0xF && operandP->vop_mode<10)) )
  972.         {        /* One byte operand. */
  973.           know( operandP->vop_mode>3 );
  974.           FRAG_APPEND_1_CHAR( operandP->vop_mode<<4 | operandP->vop_reg);
  975.           /* All 1-bytes except S^# happen here. */
  976.         }
  977.           else
  978.         {        /* {@}{q^}foo{(Rn)} or S^#foo */
  979.           if (operandP -> vop_reg == -1 && operandP -> vop_short != 's')
  980.             {        /* "{@}{q^}foo" */
  981.               if ( to_seg == now_seg )
  982.             {
  983.               if (length == 0)
  984.                 {
  985.                   know( operandP -> vop_short == ' ' );
  986.                   p = frag_var (rs_machine_dependent, 10, 2,
  987.                        ENCODE_RELAX( STATE_PC_RELATIVE, STATE_BYTE ),
  988.                         this_add_symbol, this_add_number,
  989.                         opcode_low_byteP);
  990.                   know( operandP -> vop_mode == 10 + at );
  991.                   * p = at << 4;
  992.                   /* At is the only context we need to carry to */
  993.                   /* other side of relax() process. */
  994.                   /* Must be in the correct bit position of VAX */
  995.                   /* operand spec. byte. */
  996.                 }
  997.               else
  998.                 {
  999.                   know( length );
  1000.                   know( operandP -> vop_short != ' ' );
  1001.                   p = frag_more (length+1);
  1002.                       /* JF is this array stuff really going to work? */
  1003.                   p[0] = 0xF | ((at + "?\12\14?\16"[length]) << 4);
  1004.                   fix_new (frag_now, p+1 - frag_now -> fr_literal,
  1005.                        length, this_add_symbol, 0,
  1006.                        this_add_number, 1);
  1007.                 }
  1008.             }
  1009.               else
  1010.             {    /* to_seg != now_seg */
  1011.               if (this_add_symbol == NULL)
  1012.                 {
  1013.                   know( to_seg == SEG_ABSOLUTE );
  1014.                   /* Do @#foo: simpler relocation than foo-.(pc) anyway. */
  1015.                   p = frag_more (5);
  1016.                   p [0] = VAX_ABSOLUTE_MODE; /* @#... */
  1017.                   md_number_to_chars (p+1, this_add_number, 4);
  1018.                   if (length && length != 4)
  1019.                 {
  1020.                   as_warn( "Length specification ignored. Address mode 9F used" );
  1021.                 }
  1022.                 }
  1023.               else
  1024.                 {
  1025.                   /* {@}{q^}other_seg */
  1026.                   know(   (length == 0 && operandP->vop_short == ' ')    \
  1027.                    || (length >  0 && operandP->vop_short != ' '));
  1028.                   if (is_undefined)
  1029.                 {
  1030.                   /*
  1031.                    * We have a SEG_UNKNOWN symbol. It might
  1032.                    * turn out to be in the same segment as
  1033.                    * the instruction, permitting relaxation.
  1034.                    */
  1035.                   p = frag_var (rs_machine_dependent, 5, 2,
  1036.                         ENCODE_RELAX( STATE_PC_RELATIVE, STATE_UNDF ),
  1037.                         this_add_symbol, this_add_number,
  1038.                         0);
  1039.                   p [0] = at << 4;
  1040.                 }
  1041.                   else
  1042.                 {
  1043.                   if (length == 0)
  1044.                     {
  1045.                       know( operandP -> vop_short == ' ' );
  1046.                       length = 4; /* Longest possible. */
  1047.                     }
  1048.                   p = frag_more (length+1);
  1049.                   p [0] = 0xF | ((at + "?\12\14?\16"[length]) << 4);
  1050.                   md_number_to_chars (p+1, this_add_number, length);
  1051.                   fix_new (frag_now,
  1052.                        p+1 - frag_now -> fr_literal,
  1053.                        length, this_add_symbol, 0,
  1054.                        this_add_number, 1);
  1055.                 }
  1056.                 }
  1057.             }
  1058.             }
  1059.           else
  1060.             {        /* {@}{q^}foo(Rn) or S^# or I^# or # */
  1061.               if ( operandP -> vop_mode < 0xA )
  1062.             {    /* # or S^# or I^# */
  1063.               /* know(   (length == 0 && operandP->vop_short == ' ')    \
  1064.                    || (length >  0 && operandP->vop_short != ' ')); */
  1065.               if (   length == 0
  1066.                   && to_seg == SEG_ABSOLUTE
  1067.                   && operandP -> vop_mode == 8 /* No '@'. */
  1068.                   && this_add_number < 64
  1069.                   && this_add_number >= 0)
  1070.                 {
  1071.                   operandP -> vop_short = 's';
  1072.                 }
  1073.               if ( operandP -> vop_short == 's' )
  1074.                 {
  1075.                   FRAG_APPEND_1_CHAR( this_add_number );
  1076.                 }
  1077.               else
  1078.                 {    /* I^#... */
  1079.                   know( nbytes );
  1080.                   p = frag_more (nbytes+1);
  1081.                   know( operandP -> vop_reg == 0xF );
  1082.                   p [0] = (operandP->vop_mode << 4) | 0xF;
  1083.                   if (to_seg == SEG_ABSOLUTE)
  1084.                 {
  1085. /*
  1086.  * If nbytes > 4, then we are scrod. We don't know if the
  1087.  * high order bytes are to be 0xFF or 0x00.
  1088.  * BSD4.2 & RMS say use 0x00. OK --- but this
  1089.  * assembler needs ANOTHER rewrite to
  1090.  * cope properly with this bug.
  1091.  */
  1092.                   md_number_to_chars (p+1, this_add_number, min( 4, nbytes ));
  1093.                   if (nbytes > 4)
  1094.                     {
  1095.                       bzero (p+5, nbytes - 4);
  1096.                     }
  1097.                 }
  1098.                   else
  1099.                 {
  1100.                   if (to_seg == SEG_BIG)
  1101.                     {
  1102. /*
  1103.  * Problem here is to get the bytes in the right order.
  1104.  * We stored our constant as LITTLENUMs, not bytes.
  1105.  */
  1106.                       LITTLENUM_TYPE * lP;
  1107.  
  1108.                       lP = floatP -> low;
  1109.                       if (nbytes & 1)
  1110.                     {
  1111.                       know( nbytes == 1 );
  1112.                       p [1] = * lP;
  1113.                     }
  1114.                       else
  1115.                     {
  1116.                       for (p ++;  nbytes; nbytes -= 2, p += 2, lP ++)
  1117.                         {
  1118.                           md_number_to_chars (p, *lP, 2);
  1119.                         }
  1120.                     }
  1121.                     }
  1122.                   else
  1123.                     {
  1124.                       fix_new (frag_now, p+1 - frag_now->fr_literal,
  1125.                            nbytes, this_add_symbol, 0,
  1126.                            this_add_number, 0);
  1127.                     }
  1128.                 }
  1129.                 }
  1130.             }
  1131.               else
  1132.             {    /* {@}{q^}foo(Rn) */
  1133.               know(   (length == 0 && operandP->vop_short == ' ')    \
  1134.                    || (length >  0 && operandP->vop_short != ' '));
  1135.               if (length == 0)
  1136.                 {
  1137.                   if ( to_seg == SEG_ABSOLUTE )
  1138.                 {
  1139.                   register long int    test;
  1140.  
  1141.                   test = this_add_number;
  1142.  
  1143.                   if(test<0) test = ~ test;
  1144.  
  1145.                   length = test & 0xffff8000 ? 4
  1146.                     : test & 0xffffff80 ? 2
  1147.                       : 1;
  1148.                 }
  1149.                   else
  1150.                 {
  1151.                   length = 4;
  1152.                 }
  1153.                 }
  1154.               p = frag_more (1 + length);
  1155.               know( operandP -> vop_reg >= 0 );
  1156.               p [0] = operandP -> vop_reg
  1157.                 | ((at | "?\12\14?\16"[length]) << 4);
  1158.               if (to_seg == SEG_ABSOLUTE)
  1159.                 {
  1160.                   md_number_to_chars( p+1, this_add_number, length );
  1161.                 }
  1162.               else
  1163.                 {
  1164.                   fix_new (frag_now, p+1 - frag_now -> fr_literal,
  1165.                        length, this_add_symbol, 0,
  1166.                        this_add_number, 0);
  1167.                 }
  1168.             }
  1169.             }
  1170.         }        /* if(single-byte-operand) */
  1171.         }
  1172.     }            /* for(operandP) */
  1173.     }                /* if(!need_pass_2&&!goofed) */
  1174. }                /* vax_assemble() */
  1175.  
  1176. /*
  1177.  *            md_estimate_size_before_relax()
  1178.  *
  1179.  * Called just before relax().
  1180.  * Any symbol that is now undefined will not become defined.
  1181.  * Return the correct fr_subtype in the frag.
  1182.  * Return the initial "guess for fr_var" to caller.
  1183.  * The guess for fr_var is ACTUALLY the growth beyond fr_fix.
  1184.  * Whatever we do to grow fr_fix or fr_var contributes to our returned value.
  1185.  * Although it may not be explicit in the frag, pretend fr_var starts with a
  1186.  * 0 value.
  1187.  */
  1188. int
  1189. md_estimate_size_before_relax (fragP, segment_type)
  1190.      register fragS *    fragP;
  1191.      register int    segment_type; /* N_DATA or N_TEXT. */
  1192. {
  1193.   register char *    p;
  1194.   register int        old_fr_fix;
  1195.  
  1196.   old_fr_fix = fragP -> fr_fix;
  1197.   switch (fragP -> fr_subtype)
  1198.     {
  1199.     case ENCODE_RELAX( STATE_PC_RELATIVE, STATE_UNDF ):
  1200.       if ((fragP -> fr_symbol -> sy_type & N_TYPE) == segment_type)
  1201.     {            /* A relaxable case. */
  1202.       fragP -> fr_subtype = ENCODE_RELAX( STATE_PC_RELATIVE, STATE_BYTE );
  1203.     }
  1204.       else
  1205.     {
  1206.       p = fragP -> fr_literal + old_fr_fix;
  1207.       p [0] |= VAX_PC_RELATIVE_MODE; /* Preserve @ bit. */
  1208.       fragP -> fr_fix += 1 + 4;
  1209.       fix_new (fragP, old_fr_fix + 1, 4, fragP -> fr_symbol, 0,
  1210.            fragP -> fr_offset, 1);
  1211.       frag_wane (fragP);
  1212.     }
  1213.       break;
  1214.  
  1215.     case ENCODE_RELAX( STATE_CONDITIONAL_BRANCH, STATE_UNDF ):
  1216.       if ((fragP -> fr_symbol -> sy_type & N_TYPE) == segment_type)
  1217.     {
  1218.       fragP -> fr_subtype = ENCODE_RELAX( STATE_CONDITIONAL_BRANCH, STATE_BYTE );
  1219.     }
  1220.       else
  1221.     {
  1222.       p = fragP -> fr_literal + old_fr_fix;
  1223.       * fragP -> fr_opcode ^= 1; /* Reverse sense of branch. */
  1224.       p [0] = 6;
  1225.       p [1] = VAX_JMP;
  1226.       p [2] = VAX_PC_RELATIVE_MODE;    /* ...(PC) */
  1227.       fragP -> fr_fix += 1 + 1+1+4;
  1228.       fix_new (fragP, old_fr_fix + 3, 4, fragP -> fr_symbol, 0,
  1229.            fragP -> fr_offset, 1);
  1230.       frag_wane (fragP);
  1231.     }
  1232.       break;
  1233.  
  1234.     case ENCODE_RELAX( STATE_COMPLEX_BRANCH, STATE_UNDF ):
  1235.       if ((fragP -> fr_symbol -> sy_type & N_TYPE) == segment_type)
  1236.     {
  1237.       fragP -> fr_subtype = ENCODE_RELAX( STATE_COMPLEX_BRANCH, STATE_WORD );
  1238.     }
  1239.       else
  1240.     {
  1241.       p = fragP -> fr_literal + old_fr_fix;
  1242.       p [0] = 2;
  1243.       p [1] = 0;
  1244.       p [2] = VAX_BRB;
  1245.       p [3] = 6;
  1246.       p [4] = VAX_JMP;
  1247.       p [5] = VAX_PC_RELATIVE_MODE;    /* ...(pc) */
  1248.       fragP -> fr_fix += 2 + 2 + 1+1+4;
  1249.       fix_new (fragP, old_fr_fix + 6, 4, fragP -> fr_symbol, 0,
  1250.            fragP -> fr_offset, 1);
  1251.       frag_wane (fragP);
  1252.     }
  1253.       break;
  1254.  
  1255.     case ENCODE_RELAX( STATE_COMPLEX_HOP, STATE_UNDF ):
  1256.       if ((fragP -> fr_symbol -> sy_type & N_TYPE) == segment_type)
  1257.     {
  1258.       fragP -> fr_subtype = ENCODE_RELAX( STATE_COMPLEX_HOP, STATE_BYTE );
  1259.     }
  1260.       else
  1261.     {
  1262.       p = fragP -> fr_literal + old_fr_fix;
  1263.       p [0] = 2;
  1264.       p [1] = VAX_BRB;
  1265.       p [2] = 6;
  1266.       p [3] = VAX_JMP;
  1267.       p [4] = VAX_PC_RELATIVE_MODE;    /* ...(pc) */
  1268.       fragP -> fr_fix += 1 + 2 + 1+1+4;
  1269.       fix_new (fragP, old_fr_fix + 5, 4, fragP -> fr_symbol, 0,
  1270.            fragP -> fr_offset, 1);
  1271.       frag_wane (fragP);
  1272.     }
  1273.       break;
  1274.  
  1275.     case ENCODE_RELAX( STATE_ALWAYS_BRANCH, STATE_UNDF ):
  1276.       if ((fragP -> fr_symbol -> sy_type & N_TYPE) == segment_type)
  1277.     {
  1278.       fragP -> fr_subtype = ENCODE_RELAX( STATE_ALWAYS_BRANCH, STATE_BYTE );
  1279.     }
  1280.       else
  1281.     {
  1282.       p = fragP -> fr_literal + old_fr_fix;
  1283.       * fragP -> fr_opcode += VAX_WIDEN_LONG;
  1284.       p [0] = VAX_PC_RELATIVE_MODE; /* ...(PC) */
  1285.       fragP -> fr_fix += 1+4;
  1286.       fix_new (fragP, old_fr_fix + 1, 4, fragP -> fr_symbol, 0,
  1287.            fragP -> fr_offset, 1);
  1288.       frag_wane (fragP);
  1289.     }
  1290.       break;
  1291.  
  1292.     default:
  1293.       break;
  1294.     }
  1295.   return (fragP -> fr_var   +   fragP -> fr_fix   -   old_fr_fix);
  1296. }                /* md_estimate_size_before_relax() */
  1297.  
  1298. /*
  1299.  *            md_convert_frag();
  1300.  *
  1301.  * Called after relax() is finished.
  1302.  * In:    Address of frag.
  1303.  *    fr_type == rs_machine_dependent.
  1304.  *    fr_subtype is what the address relaxed to.
  1305.  *
  1306.  * Out:    Any fixSs and constants are set up.
  1307.  *    Caller will turn frag into a ".space 0".
  1308.  */
  1309. void
  1310. md_convert_frag (fragP)
  1311.      register fragS *    fragP;
  1312. {
  1313.   register char *        addressP; /* -> _var to change. */
  1314.   register char *        opcodeP; /* -> opcode char(s) to change. */
  1315.   register short int        length_code; /* 2=long 1=word 0=byte */
  1316.   register short int        extension; /* Size of relaxed address. */
  1317.                 /* Added to fr_fix: incl. ALL var chars. */
  1318.   register symbolS *        symbolP;
  1319.   register long int        where;
  1320.   register long int        address_of_var;
  1321.                 /* Where, in file space, is _var of *fragP? */
  1322.   register long int        target_address;
  1323.                 /* Where, in file space, does addr point? */
  1324.  
  1325.   know (fragP -> fr_type == rs_machine_dependent );
  1326.   length_code = fragP -> fr_subtype & 3; /* depends on ENCODE_RELAX() */
  1327.   know( length_code >= 0 && length_code < 3 );
  1328.   where = fragP -> fr_fix;
  1329.   addressP = fragP -> fr_literal + where;
  1330.   opcodeP = fragP -> fr_opcode;
  1331.   symbolP = fragP -> fr_symbol;
  1332.   know( symbolP );
  1333.   target_address = symbolP -> sy_value + fragP -> fr_offset;
  1334.   address_of_var = fragP -> fr_address + where;
  1335.   switch ( fragP -> fr_subtype )
  1336.     {
  1337.     case ENCODE_RELAX( STATE_PC_RELATIVE, STATE_BYTE):
  1338.       know( *addressP == 0 || *addressP == 0x10 );    /* '@' bit. */
  1339.       addressP [0] |= 0xAF; /* Byte displacement. */
  1340.       addressP [1] = target_address - (address_of_var + 2);
  1341.       extension = 2;
  1342.       break;
  1343.  
  1344.     case ENCODE_RELAX( STATE_PC_RELATIVE, STATE_WORD):
  1345.       know( *addressP == 0 || *addressP == 0x10 );    /* '@' bit. */
  1346.       addressP [0] |= 0xCF; /* Word displacement. */
  1347.       md_number_to_chars (addressP+1, target_address - (address_of_var + 3), 2);
  1348.       extension = 3;
  1349.       break;
  1350.  
  1351.     case ENCODE_RELAX( STATE_PC_RELATIVE, STATE_LONG):
  1352.       know( *addressP == 0 || *addressP == 0x10 );    /* '@' bit. */
  1353.       addressP [0] |= 0xEF; /* Long word displacement. */
  1354.       md_number_to_chars (addressP+1, target_address - (address_of_var + 5), 4);
  1355.       extension = 5;
  1356.       break;
  1357.  
  1358.     case ENCODE_RELAX( STATE_CONDITIONAL_BRANCH, STATE_BYTE):
  1359.       addressP [0] = target_address - (address_of_var + 1);
  1360.       extension = 1;
  1361.       break;
  1362.  
  1363.     case ENCODE_RELAX( STATE_CONDITIONAL_BRANCH, STATE_WORD):
  1364.       opcodeP  [0] ^= 1;        /* Reverse sense of test. */
  1365.       addressP [0] = 3;
  1366.       addressP [1] = VAX_BRB + VAX_WIDEN_WORD;
  1367.       md_number_to_chars (addressP+2, target_address - (address_of_var + 4), 2);
  1368.       extension = 4;
  1369.       break;
  1370.  
  1371.     case ENCODE_RELAX( STATE_CONDITIONAL_BRANCH, STATE_LONG):
  1372.       opcodeP  [0] ^= 1;        /* Reverse sense of test. */
  1373.       addressP [0] = 6;
  1374.       addressP [1] = VAX_JMP;
  1375.       addressP [2] = VAX_PC_RELATIVE_MODE;
  1376.       md_number_to_chars (addressP+3, target_address, 4);
  1377.       extension = 7;
  1378.       break;
  1379.  
  1380.     case ENCODE_RELAX( STATE_ALWAYS_BRANCH, STATE_BYTE):
  1381.       addressP [0] = target_address - (address_of_var + 1);
  1382.       extension = 1;
  1383.       break;
  1384.  
  1385.     case ENCODE_RELAX( STATE_ALWAYS_BRANCH, STATE_WORD):
  1386.       opcodeP [0] += VAX_WIDEN_WORD;    /* brb -> brw, bsbb -> bsbw */
  1387.       md_number_to_chars (addressP, target_address - (address_of_var + 2), 2);
  1388.       extension = 2;
  1389.       break;
  1390.  
  1391.     case ENCODE_RELAX( STATE_ALWAYS_BRANCH, STATE_LONG):
  1392.       opcodeP [0] += VAX_WIDEN_LONG;    /* brb -> jmp, bsbb -> jsb */
  1393.       addressP [0] = VAX_PC_RELATIVE_MODE;
  1394.       md_number_to_chars (addressP+1, target_address - (address_of_var + 5), 4);
  1395.       extension = 5;
  1396.       break;
  1397.  
  1398.     case ENCODE_RELAX( STATE_COMPLEX_BRANCH, STATE_WORD):
  1399.       md_number_to_chars (addressP, target_address - (address_of_var + 2), 2);
  1400.       extension = 2;
  1401.       break;
  1402.  
  1403.     case ENCODE_RELAX( STATE_COMPLEX_BRANCH, STATE_LONG):
  1404.       addressP [0] = 2;
  1405.       addressP [1] = 0;
  1406.       addressP [2] = VAX_BRB;
  1407.       addressP [3] = 6;
  1408.       addressP [4] = VAX_JMP;
  1409.       addressP [5] = VAX_PC_RELATIVE_MODE;
  1410.       md_number_to_chars (addressP+6, target_address, 4);
  1411.       extension = 10;
  1412.       break;
  1413.  
  1414.     case ENCODE_RELAX( STATE_COMPLEX_HOP, STATE_BYTE):
  1415.       addressP [0] = target_address - (address_of_var + 1);
  1416.       extension = 1;
  1417.       break;
  1418.  
  1419.     case ENCODE_RELAX( STATE_COMPLEX_HOP, STATE_WORD):
  1420.       addressP [0] = 2;
  1421.       addressP [1] = VAX_BRB;
  1422.       addressP [2] = 3;
  1423.       addressP [3] = VAX_BRW;
  1424.       md_number_to_chars (addressP+4, target_address - (address_of_var + 6), 2);
  1425.       extension = 6;
  1426.       break;
  1427.  
  1428.     case ENCODE_RELAX( STATE_COMPLEX_HOP, STATE_LONG):
  1429.       addressP [0] = 2;
  1430.       addressP [1] = VAX_BRB;
  1431.       addressP [2] = 6;
  1432.       addressP [3] = VAX_JMP;
  1433.       addressP [4] = VAX_PC_RELATIVE_MODE;
  1434.       md_number_to_chars (addressP+5, target_address, 4);
  1435.       extension = 9;
  1436.       break;
  1437.  
  1438.     default:
  1439.       BAD_CASE( fragP -> fr_subtype );
  1440.       break;
  1441.     }
  1442.   fragP -> fr_fix += extension;
  1443. }
  1444.  
  1445. /* the bit-field entries in the relocation_info struct plays hell 
  1446.    with the byte-order problems of cross-assembly.  So as a hack,
  1447.    I added this mach. dependent ri twiddler.  Ugly, but it gets
  1448.    you there. -KWK */
  1449. /* on vax: first 4 bytes are normal unsigned long, next three bytes
  1450.    are symbolnum, least sig. byte first.  Last byte is broken up with
  1451.    the upper nibble as nuthin, bit 3 as extern, bits 2 & 1 as length, and
  1452.    bit 0 as pcrel. */
  1453. void md_ri_to_chars(ri_p, ri)
  1454.      struct relocation_info *ri_p, ri;
  1455. {
  1456.   unsigned char the_bytes[8];
  1457.   
  1458.   /* this is easy */
  1459.   md_number_to_chars(the_bytes, ri.r_address, sizeof(ri.r_address));
  1460.   /* now the fun stuff */
  1461.   the_bytes[6] = (ri.r_symbolnum >> 16) & 0x0ff;
  1462.   the_bytes[5] = (ri.r_symbolnum >> 8) & 0x0ff;
  1463.   the_bytes[4] = ri.r_symbolnum & 0x0ff;
  1464.   the_bytes[7] = (((ri.r_extern << 3)  & 0x08) | ((ri.r_length << 1) & 0x06) | 
  1465.     ((ri.r_pcrel << 0)  & 0x01)) & 0x0F; 
  1466.   /* now put it back where you found it */
  1467.   bcopy (the_bytes, (char *)ri_p, sizeof(struct relocation_info));
  1468. }
  1469.  
  1470.  
  1471.  
  1472.     /* JF this used to be a separate file */
  1473. /* vax_ins_parse.c - a part for a VAX assembler */
  1474.  
  1475. /* Copyright (C) 1987 Free Software Foundtation, Inc */
  1476.  
  1477. /*
  1478.  *       BUGS, GRIPES,  APOLOGIA, etc.
  1479.  *
  1480.  * The opcode table 'votstrs' needs to be sorted on opcode frequency.
  1481.  * That is, AFTER we hash it with hash_...(), we want most-used opcodes
  1482.  * to come out of the hash table faster.
  1483.  *
  1484.  * I am sorry to inflict
  1485.  * yet another VAX assembler on the world, but RMS says we must
  1486.  * do everything from scratch, to prevent pin-heads restricting
  1487.  * this software.
  1488.  */
  1489.  
  1490. /*
  1491.  * This is a vaguely modular set of routines in C to parse VAX
  1492.  * assembly code using DEC mnemonics. It is NOT un*x specific.
  1493.  *
  1494.  * The idea here is that the assembler has taken care of all:
  1495.  *   labels
  1496.  *   macros
  1497.  *   listing
  1498.  *   pseudo-ops
  1499.  *   line continuation
  1500.  *   comments
  1501.  *   condensing any whitespace down to exactly one space
  1502.  * and all we have to do is parse 1 line into a vax instruction
  1503.  * partially formed. We will accept a line, and deliver:
  1504.  *   an error message (hopefully empty)
  1505.  *   a skeleton VAX instruction (tree structure)
  1506.  *   textual pointers to all the operand expressions
  1507.  *   a warning message that notes a silly operand (hopefully empty)
  1508.  */
  1509.  
  1510. /*
  1511.  *        E D I T   H I S T O R Y
  1512.  *
  1513.  * 17may86 Dean Elsner. Bug if line ends immediately after opcode.
  1514.  * 30apr86 Dean Elsner. New vip_op() uses arg block so change call.
  1515.  *  6jan86 Dean Elsner. Crock vip_begin() to call vip_op_defaults().
  1516.  *  2jan86 Dean Elsner. Invent synthetic opcodes.
  1517.  *    Widen vax_opcodeT to 32 bits. Use a bit for VIT_OPCODE_SYNTHETIC,
  1518.  *    which means this is not a real opcode, it is like a macro; it will
  1519.  *    be relax()ed into 1 or more instructions.
  1520.  *    Use another bit for VIT_OPCODE_SPECIAL if the op-code is not optimised
  1521.  *    like a regular branch instruction. Option added to vip_begin():
  1522.  *    exclude    synthetic opcodes. Invent synthetic_votstrs[].
  1523.  * 31dec85 Dean Elsner. Invent vit_opcode_nbytes.
  1524.  *    Also make vit_opcode into a char[]. We now have n-byte vax opcodes,
  1525.  *    so caller's don't have to know the difference between a 1-byte & a
  1526.  *    2-byte op-code. Still need vax_opcodeT concept, so we know how
  1527.  *    big an object must be to hold an op.code.
  1528.  * 30dec85 Dean Elsner. Widen typedef vax_opcodeT in "vax-inst.h"
  1529.  *    because vax opcodes may be 16 bits. Our crufty C compiler was
  1530.  *    happily initialising 8-bit vot_codes with 16-bit numbers!
  1531.  *    (Wouldn't the 'phone company like to compress data so easily!)
  1532.  * 29dec85 Dean Elsner. New static table vax_operand_width_size[].
  1533.  *    Invented so we know hw many bytes a "I^#42" needs in its immediate
  1534.  *    operand. Revised struct vop in "vax-inst.h": explicitly include
  1535.  *    byte length of each operand, and it's letter-code datum type.
  1536.  * 17nov85 Dean Elsner. Name Change.
  1537.  *    Due to ar(1) truncating names, we learned the hard way that
  1538.  *    "vax-inst-parse.c" -> "vax-inst-parse." dropping the "o" off
  1539.  *    the archived object name. SO... we shortened the name of this
  1540.  *    source file, and changed the makefile.
  1541.  */
  1542.  
  1543. /* #include <stdio.h> JF for one big happy file */
  1544.  
  1545. /* JF #include "vax-inst.h"        /* define the tree we parse it into */
  1546.  
  1547.  
  1548. static char *   op_hash = NULL;    /* handle of the OPCODE hash table */
  1549.                 /* NULL means any use before vip_begin() */
  1550.                 /* will crash */
  1551.  
  1552. /*
  1553.  * In:    1 character, from "bdfghloqpw" being the data-type of an operand
  1554.  *    of a vax instruction.
  1555.  *
  1556.  * Out:    the length of an operand of that type, in bytes.
  1557.  *    Special branch operands types "-?!" have length 0.
  1558.  */
  1559.  
  1560. static short int vax_operand_width_size [256] = {
  1561.  
  1562. #define _ 0
  1563.  _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
  1564.  _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
  1565.  _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
  1566.  _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
  1567.  _, _, 1, _, 8, _, 4, 8,16, _, _, _, 4, _, _,16, /* ..b.d.fgh...l..o */
  1568.  _, 8, _, _, _, _, _, 2, _, _, _, _, _, _, _, _, /* .q.....w........ */
  1569.  _, _, 1, _, 8, _, 4, 8,16, _, _, _, 4, _, _,16, /* ..b.d.fgh...l..o */
  1570.  _, 8, _, _, _, _, _, 2, _, _, _, _, _, _, _, _, /* .q.....w........ */
  1571.  _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
  1572.  _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
  1573.  _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
  1574.  _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
  1575.  _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
  1576.  _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
  1577.  _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
  1578.  _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _};
  1579. #undef _
  1580.  
  1581. /*
  1582.  * This perversion encodes all the vax opcodes as a bunch of strings.
  1583.  * RMS says we should build our hash-table at run-time. Hmm.
  1584.  * Please would someone arrange these in decreasing frequency of opcode?
  1585.  * Because of the way hash_...() works, the most frequently used opcode
  1586.  * should be textually first and so on.
  1587.  *
  1588.  * Input for this table was 'vax.opcodes', awk(1)ed by 'vax.opcodes.c.awk' .
  1589.  * So change 'vax.opcodes', then re-generate this table.
  1590.  */
  1591.  
  1592. #include "vax-opcode.h"
  1593.  
  1594. /*
  1595.  * This is a table of optional op-codes. All of them represent
  1596.  * 'synthetic' instructions that seem popular.
  1597.  *
  1598.  * Here we make some pseudo op-codes. Every code has a bit set to say
  1599.  * it is synthetic. This lets you catch them if you want to
  1600.  * ban these opcodes. They are mnemonics for "elastic" instructions
  1601.  * that are supposed to assemble into the fewest bytes needed to do a
  1602.  * branch, or to do a conditional branch, or whatever.
  1603.  *
  1604.  * The opcode is in the usual place [low-order n*8 bits]. This means
  1605.  * that if you mask off the bucky bits, the usual rules apply about
  1606.  * how long the opcode is.
  1607.  *
  1608.  * All VAX branch displacements come at the end of the instruction.
  1609.  * For simple branches (1-byte opcode + 1-byte displacement) the last
  1610.  * operand is coded 'b?' where the "data type" '?' is a clue that we
  1611.  * may reverse the sense of the branch (complement lowest order bit)
  1612.  * and branch around a jump. This is by far the most common case.
  1613.  * That is why the VIT_OPCODE_SYNTHETIC bit is set: it says this is
  1614.  * a 0-byte op-code followed by 2 or more bytes of operand address.
  1615.  *
  1616.  * If the op-code has VIT_OPCODE_SPECIAL set, then we have a more unusual
  1617.  * case.
  1618.  *
  1619.  * For JBSB & JBR the treatment is the similar, except (1) we have a 'bw'
  1620.  * option before (2) we can directly JSB/JMP because there is no condition.
  1621.  * These operands have 'b-' as their access/data type.
  1622.  *
  1623.  * That leaves a bunch of random opcodes: JACBx, JxOBxxx. In these
  1624.  * cases, we do the same idea. JACBxxx are all marked with a 'b!'
  1625.  * JAOBxxx & JSOBxxx are marked with a 'b:'.
  1626.  *
  1627.  */
  1628. #if (VIT_OPCODE_SYNTHETIC != 0x80000000)
  1629. You have just broken the encoding below, which assumes the sign bit
  1630. means 'I am an imaginary instruction'.
  1631. #endif
  1632.  
  1633. #if (VIT_OPCODE_SPECIAL != 0x40000000)
  1634. You have just broken the encoding below, which assumes the 0x40M bit means
  1635. 'I am not to be "optimised" the way normal branches are'.
  1636. #endif
  1637.  
  1638. static struct vot
  1639. synthetic_votstrs[] =
  1640. {
  1641. {    "jbsb",    {"b-",            0xC0000010    } }, /* BSD 4.2 */
  1642.                                 /* jsb used already */
  1643. {    "jbr",    {"b-",            0xC0000011    } }, /* BSD 4.2 */
  1644. {    "jr",    {"b-",            0xC0000011    } }, /* consistent */
  1645. {    "jneq",    {"b?",            0x80000012    } },
  1646. {    "jnequ",    {"b?",            0x80000012    } },
  1647. {    "jeql",    {"b?",            0x80000013    } },
  1648. {    "jeqlu",    {"b?",            0x80000013    } },
  1649. {    "jgtr",    {"b?",            0x80000014    } },
  1650. {    "jleq",    {"b?",            0x80000015    } },
  1651.                                         /* un-used opcodes here */
  1652. {    "jgeq",    {"b?",            0x80000018    } },
  1653. {    "jlss",    {"b?",            0x80000019    } },
  1654. {    "jgtru",    {"b?",            0x8000001a    } },
  1655. {    "jlequ",    {"b?",            0x8000001b    } },
  1656. {    "jvc",    {"b?",            0x8000001c    } },
  1657. {    "jvs",    {"b?",            0x8000001d    } },
  1658. {    "jgequ",    {"b?",            0x8000001e    } },
  1659. {    "jcc",    {"b?",            0x8000001e    } },
  1660. {    "jlssu",    {"b?",            0x8000001f    } },
  1661. {    "jcs",    {"b?",            0x8000001f    } },
  1662.  
  1663. {    "jacbw",    {"rwrwmwb!",        0xC000003d    } },
  1664. {    "jacbf",    {"rfrfmfb!",        0xC000004f    } },
  1665. {    "jacbd",    {"rdrdmdb!",        0xC000006f    } },
  1666. {    "jacbb",    {"rbrbmbb!",        0xC000009d    } },
  1667. {    "jacbl",    {"rlrlmlb!",        0xC00000f1    } },
  1668. {    "jacbg",    {"rgrgmgb!",        0xC0004ffd    } },
  1669. {    "jacbh",    {"rhrhmhb!",        0xC0006ffd    } },
  1670.  
  1671. {    "jbs",    {"rlvbb?",        0x800000e0    } },
  1672. {    "jbc",    {"rlvbb?",        0x800000e1    } },
  1673. {    "jbss",    {"rlvbb?",        0x800000e2    } },
  1674. {    "jbcs",    {"rlvbb?",        0x800000e3    } },
  1675. {    "jbsc",    {"rlvbb?",        0x800000e4    } },
  1676. {    "jbcc",    {"rlvbb?",        0x800000e5    } },
  1677. {    "jbssi",    {"rlvbb?",        0x800000e6    } },
  1678. {    "jbcci",    {"rlvbb?",        0x800000e7    } },
  1679. {    "jlbs",    {"rlb?",        0x800000e8    } },    /* JF changed from rlvbb? */
  1680. {    "jlbc",    {"rlb?",        0x800000e9    } },    /* JF changed from rlvbb? */
  1681.  
  1682. {    "jaoblss",    {"rlmlb:",        0xC00000f2    } },
  1683. {    "jaobleq",    {"rlmlb:",        0xC00000f3    } },
  1684. {    "jsobgeq",    {"mlb:",        0xC00000f4    } },/* JF was rlmlb: */
  1685. {    "jsobgtr",    {"mlb:",        0xC00000f5    } },/* JF was rlmlb: */
  1686.  
  1687. /* CASEx has no branch addresses in our conception of it. */
  1688. /* You should use ".word ..." statements after the "case ...". */
  1689.  
  1690. {      ""       ,   ""          } /* empty is end sentinel */
  1691.  
  1692. };                /* synthetic_votstrs */
  1693.  
  1694. /*
  1695.  *                  v i p _ b e g i n ( )
  1696.  *
  1697.  * Call me once before you decode any lines.
  1698.  * I decode votstrs into a hash table at op_hash (which I create).
  1699.  * I return an error text: hopefully "".
  1700.  * If you want, I will include the 'synthetic' jXXX instructions in the
  1701.  * instruction table.
  1702.  * You must nominate metacharacters for eg DEC's "#", "@", "^".
  1703.  */
  1704.  
  1705. char *
  1706. vip_begin(synthetic_too, immediate, indirect, displen)
  1707. int    synthetic_too;        /* TRUE means include jXXX op-codes. */
  1708. char *    immediate,
  1709.      *    indirect,
  1710.      *    displen;
  1711. {
  1712.   register struct vot * vP;        /* scan votstrs */
  1713.   register char *       retval;        /* error text */
  1714.  
  1715.            char *       hash_insert();    /*  */
  1716.            char *       hash_new();    /* lies */
  1717.   
  1718.   if ( (op_hash = hash_new()) )
  1719.     {
  1720.       retval = "";            /* OK so far */
  1721.       for (vP=votstrs; *vP->vot_name && !*retval; vP++)
  1722.     {
  1723.       retval = hash_insert(op_hash, vP->vot_name, &vP->vot_detail);
  1724.     }
  1725.       if ( synthetic_too )
  1726.     {
  1727.       for (vP=synthetic_votstrs; *vP->vot_name && !*retval; vP++)
  1728.         {
  1729.           retval = hash_insert(op_hash, vP->vot_name, &vP->vot_detail);
  1730.         }
  1731.     }
  1732.     }
  1733.   else
  1734.     {
  1735.       retval="virtual memory exceeded";
  1736.     }
  1737.   vip_op_defaults (immediate, indirect, displen);
  1738.  
  1739.   return (retval);
  1740. }
  1741.  
  1742.  
  1743. /*
  1744.  *                  v i p _ e n d ( )
  1745.  *
  1746.  * Call me once after you have decoded all lines.
  1747.  * I do any cleaning-up needed.
  1748.  *
  1749.  * We don't have to do any cleanup ourselves: all of our operand
  1750.  * symbol table is static, and free()ing it is naughty.
  1751.  */
  1752. vip_end()
  1753. {
  1754. }
  1755.  
  1756. /*
  1757.  *                  v i p ( )
  1758.  *
  1759.  * This converts a string into a vax instruction.
  1760.  * The string must be a bare single instruction in dec-vax (with BSD4 frobs)
  1761.  * format.
  1762.  * It provides some error messages: at most one fatal error message (which
  1763.  * stops the scan) and at most one warning message for each operand.
  1764.  * The vax instruction is returned in exploded form, since we have no
  1765.  * knowledge of how you parse (or evaluate) your expressions.
  1766.  * We do however strip off and decode addressing modes and operation
  1767.  * mnemonic.
  1768.  *
  1769.  * The exploded instruction is returned to a struct vit of your choice.
  1770.  * #include "vax-inst.h" to know what a struct vit is.
  1771.  *
  1772.  * This function's value is a string. If it is not "" then an internal
  1773.  * logic error was found: read this code to assign meaning to the string.
  1774.  * No argument string should generate such an error string:
  1775.  * it means a bug in our code, not in the user's text.
  1776.  *
  1777.  * You MUST have called vip_begin() once and vip_end() never before using
  1778.  * this function.
  1779.  */
  1780.  
  1781. char *                /* "" or bug string */
  1782. vip (vitP, instring)
  1783.      struct vit *    vitP;    /* We build an exploded instruction here. */
  1784.      char *        instring; /* Text of a vax instruction: we modify. */
  1785. {
  1786.   register struct vot_wot *    vwP;    /* How to bit-encode this opcode. */
  1787.   register char *        p;    /* 1/skip whitespace.2/scan vot_how */
  1788.   register char *        q;    /*  */
  1789.   register char *        bug;    /* "" or program logic error */
  1790.   register unsigned char    count;    /* counts number of operands seen */
  1791.   register struct vop *        operandp; /* scan operands in struct vit */
  1792.   register char *        alloperr; /* error over all operands */
  1793.   register char            c;    /* Remember char, (we clobber it */
  1794.                     /* with '\0' temporarily). */
  1795.   register vax_opcodeT        oc;     /* Op-code of this instruction. */
  1796.  
  1797.   struct vot_wot *        hash_find();
  1798.   char *            vip_op();
  1799.  
  1800.   bug = "";
  1801.   if (* instring == ' ')
  1802.       ++ instring;        /* Skip leading whitespace. */
  1803.   for ( p=instring; *p && *p!=' '; p++)
  1804.     ;                /* MUST end in end-of-string or exactly 1 space. */
  1805.                     /* Scanned up to end of operation-code. */
  1806.                 /* Operation-code is ended with whitespace. */
  1807.   if (p-instring == 0)
  1808.     {
  1809.       vitP -> vit_error    = "No operator";
  1810.       count           = 0;
  1811.       bzero (vitP -> vit_opcode, sizeof(vitP -> vit_opcode) );
  1812.     }
  1813.   else
  1814.     {
  1815.       c = *p;
  1816.       *p = '\0';
  1817.       /*
  1818.        * Here with instring pointing to what better be an op-name, and p
  1819.        * pointing to character just past that.
  1820.        * We trust instring points to an op-name, with no whitespace.
  1821.        */
  1822.       vwP = hash_find (op_hash, instring);
  1823.       * p = c;            /* Restore char after op-code. */
  1824.       if ( vwP == 0)
  1825.     {
  1826.       vitP -> vit_error     = "Unknown operator";
  1827.       count                 = 0;
  1828.       bzero (vitP -> vit_opcode, sizeof(vitP -> vit_opcode) );
  1829.     }
  1830.       else
  1831.     {
  1832.       /*
  1833.        * We found a match! So lets pick up as many operands as the
  1834.        * instruction wants, and even gripe if there are too many.
  1835.        * We expect comma to seperate each operand.
  1836.        * We let instring track the text, while p tracks a part of the
  1837.        * struct vot.
  1838.        */
  1839.       /*
  1840.        * The lines below know about 2-byte opcodes starting FD,FE or FF.
  1841.        * They also understand synthetic opcodes. Note:
  1842.        * we return 32 bits of opcode, including bucky bits, BUT
  1843.        * an opcode length is either 8 or 16 bits for vit_opcode_nbytes.
  1844.        */
  1845.       oc = vwP -> vot_code;    /* The op-code. */
  1846.       vitP -> vit_opcode_nbytes = (oc & 0xFF) >= 0xFD ? 2 : 1;
  1847.       md_number_to_chars (vitP -> vit_opcode, oc, 4);
  1848.       count = 0;        /* no operands seen yet */
  1849.       instring = p;        /* point just past operation code */
  1850.       alloperr = "";
  1851.       for ( p = vwP -> vot_how,  operandp = vitP -> vit_operand;
  1852.            !*alloperr && !*bug && *p;
  1853.            operandp++, p += 2
  1854.            )
  1855.         {
  1856.           /*
  1857.            * Here to parse one operand. Leave instring pointing just
  1858.            * past any one ',' that marks the end of this operand.
  1859.            */
  1860.           if (!p[1])
  1861.           bug = "p";    /* ODD(!!) number of bytes in vot_how?? */
  1862.           else if (*instring)
  1863.         {
  1864.           for ( q=instring; (c = *q) && c!=','; q++)
  1865.             ;
  1866.           /*
  1867.            * Q points to ',' or '\0' that ends argument. C is that
  1868.            * character.
  1869.            */
  1870.           *q = 0;
  1871.           operandp -> vop_width  = p[1];
  1872.           operandp -> vop_nbytes = vax_operand_width_size [p [1]];
  1873.           operandp -> vop_access = p[0];
  1874.           bug = vip_op (instring, operandp);
  1875.           *q = c;    /* Restore input text. */
  1876.           if ( * (operandp->vop_error)  )
  1877.             alloperr = "Bad operand";
  1878.           instring = q + (c?1:0); /* next operand (if any) */
  1879.           count++;    /*  won another argument, may have an operr */
  1880.         }
  1881.           else
  1882.           alloperr = "Not enough operands";
  1883.         }
  1884.       if (!*alloperr)
  1885.         {
  1886.           if (* instring == ' ')
  1887.           instring++;    /* Skip whitespace. */
  1888.           if (*instring)
  1889.           alloperr = "Too many operands";
  1890.         }
  1891.       vitP -> vit_error = alloperr;
  1892.     }
  1893.     }
  1894.   vitP -> vit_operands = count;
  1895.   return(bug);
  1896. }
  1897.  
  1898. #ifdef test
  1899.  
  1900. /*
  1901.  * Test program for above.
  1902.  */
  1903.  
  1904. struct vit    myvit;        /* build an exploded vax instruction here */
  1905. char        answer[100];    /* human types a line of vax assembler here */
  1906. char *        mybug;        /* "" or an internal logic diagnostic */
  1907. int        mycount;    /* number of operands */
  1908. struct vop *    myvop;        /* scan operands from myvit */
  1909. int        mysynth;    /* TRUE means want synthetic opcodes. */
  1910. char    my_immediate[200];
  1911. char    my_indirect[200];
  1912. char    my_displen[200];
  1913.  
  1914. char *        vip();
  1915.  
  1916. main()
  1917. {
  1918.   char *    p;
  1919.   char *    vip_begin();
  1920.  
  1921.   printf("0 means no synthetic instructions.   ");
  1922.   printf("Value for vip_begin?  ");
  1923.   gets(answer);
  1924.   sscanf(answer,"%d",&mysynth);
  1925.   printf("Synthetic opcodes %s be included.\n", mysynth ? "will" : "will not" );
  1926.   printf("enter immediate symbols eg enter #   ");
  1927.   gets(my_immediate);
  1928.   printf("enter indirect symbols  eg enter @   ");
  1929.   gets(my_indirect);
  1930.   printf("enter displen symbols   eg enter ^   ");
  1931.   gets(my_displen);
  1932.   if ( * (p = vip_begin (mysynth, my_immediate, my_indirect, my_displen) ) )
  1933.     {
  1934.       error("vip_begin=%s",p);
  1935.     }
  1936.   printf("An empty input line will quit you from the vax instruction parser\n");
  1937.   for(;;)
  1938.     {
  1939.       printf ("vax instruction: ");
  1940.       fflush (stdout);
  1941.       gets (answer);
  1942.       if (!*answer)
  1943.     {
  1944.       break;        /* out of for each input text loop */
  1945.     }
  1946.       mybug = vip (&myvit, answer);
  1947.       if (*mybug)
  1948.     {
  1949.       printf ("BUG:\"%s\"\n",mybug);
  1950.     }
  1951.       if (*myvit.vit_error)
  1952.     {
  1953.       printf ("ERR:\"%s\"\n",myvit.vit_error);
  1954.     }
  1955.       printf ("opcode=");
  1956.       for (mycount=myvit . vit_opcode_nbytes, p = myvit . vit_opcode;
  1957.        mycount;
  1958.        mycount--, p++
  1959.        )
  1960.       {
  1961.     printf ("%02x ", *p&0xFF);
  1962.       }
  1963.       printf ("   operand count=%d.\n", mycount = myvit.vit_operands);
  1964.       for (myvop=myvit.vit_operand; mycount; mycount--,myvop++)
  1965.     {
  1966.       printf ("mode=%xx reg=%xx ndx=%xx len='%c'=%c%c%d. expr=\"",
  1967.           myvop->vop_mode, myvop->vop_reg, myvop->vop_ndx,
  1968.           myvop->vop_short, myvop->vop_access, myvop-> vop_width,
  1969.           myvop->vop_nbytes);
  1970.       for (p=myvop->vop_expr_begin; p<=myvop->vop_expr_end; p++)
  1971.         {
  1972.           putchar(*p);
  1973.         }
  1974.       printf("\"\n");
  1975.       if (*myvop->vop_error)
  1976.         {
  1977.           printf ("  err:\"%s\"\n", myvop->vop_error);
  1978.         }
  1979.       if (*myvop->vop_warn)
  1980.         {
  1981.           printf ("  wrn:\"%s\"\n", myvop->vop_warn);
  1982.         }
  1983.     }
  1984.     }
  1985.   vip_end();
  1986.   exit();
  1987. }
  1988. #endif /* #ifdef test */
  1989.  
  1990. /* end of vax_ins_parse.c */
  1991.  
  1992.         /* JF this used to be a separate file also */
  1993. /* vax_reg_parse.c - convert a VAX register name to a number */
  1994.  
  1995. /* Copyright (C) 1987 Free Software Foundation, Inc. A part of GNU. */
  1996.  
  1997. /*
  1998.  *          v a x _ r e g _ p a r s e ( )
  1999.  *
  2000.  * Take 3 char.s, the last of which may be `\0` (non-existent)
  2001.  * and return the VAX register number that they represent.
  2002.  *
  2003.  * Return -1 if they don't form a register name. Good names return
  2004.  * a number from 0:15 inclusive.
  2005.  *
  2006.  * Case is not important in a name.
  2007.  *
  2008.  * Register names understood are:
  2009.  *
  2010.  *    R0
  2011.  *    R1
  2012.  *    R2
  2013.  *    R3
  2014.  *    R4
  2015.  *    R5
  2016.  *    R6
  2017.  *     R7
  2018.  *    R8
  2019.  *    R9
  2020.  *    R10
  2021.  *    R11
  2022.  *    R12    AP
  2023.  *    R13    FP
  2024.  *    R14    SP
  2025.  *    R15    PC
  2026.  *
  2027.  */
  2028.  
  2029. #include <ctype.h>
  2030. #define AP (12)
  2031. #define FP (13)
  2032. #define SP (14)
  2033. #define PC (15)
  2034.  
  2035. int                /* return -1 or 0:15 */
  2036. vax_reg_parse(c1, c2, c3)    /* 3 chars of register name */
  2037.      char c1, c2, c3;        /* c3 == 0 if 2-character reg name */
  2038. {
  2039.   register int   retval;    /* return -1:15 */
  2040.  
  2041.   retval = -1;
  2042.  
  2043.   if (isupper(c1)) c1 = tolower(c1);
  2044.   if (isupper(c2)) c2 = tolower(c2);
  2045.   if (isdigit(c2) && c1=='r')
  2046.     {
  2047.       retval = c2 - '0';
  2048.       if (isdigit(c3))
  2049.     {
  2050.       retval = retval*10 + c3 - '0';
  2051.       retval = (retval>15) ? -1 : retval;
  2052.                 /* clamp the register value to 1 hex digit */
  2053.     }
  2054.       else if (c3)
  2055.     retval = -1;    /* c3 must be '\0' or a digit */
  2056.     }
  2057.   else if (c3)        /* There are no three letter regs */
  2058.       retval = -1;
  2059.   else if (c2=='p')
  2060.     {
  2061.       switch (c1)
  2062.     {
  2063.     case 's':
  2064.       retval = SP;
  2065.       break;
  2066.     case 'f':
  2067.       retval = FP;
  2068.       break;
  2069.     case 'a':
  2070.       retval = AP;
  2071.       break;
  2072.     default:
  2073.       retval = -1;
  2074.     }
  2075.     }
  2076.   else if (c1=='p' && c2=='c')
  2077.       retval = PC;
  2078.   else
  2079.       retval = -1;
  2080.   return (retval);
  2081. }
  2082.  
  2083. /* end: vax_reg_parse.c */
  2084.  
  2085.             /* JF this was another separate prog */
  2086. /* vip_op.c - parse 1 VAX instr's operand.(C)1986 Free Software Foundation. */
  2087.  
  2088. /* JF #include <ctype.h> */
  2089. /* #include "vax_inst.h" */
  2090.  
  2091. /*
  2092.  *               v i p _ o p ( )
  2093.  *
  2094.  * Parse a vax operand in DEC assembler notation.
  2095.  * For speed, expect a string of whitespace to be reduced to a single ' '.
  2096.  * This is the case for GNU AS, and is easy for other DEC-compatible
  2097.  * assemblers.
  2098.  *
  2099.  * Knowledge about DEC VAX assembler operand notation lives here.
  2100.  * This doesn't even know what a register name is, except it believes
  2101.  * all register names are 2 or 3 characters, and lets vax_reg_parse() say
  2102.  * what number each name represents.
  2103.  * It does, however, know that PC, SP etc are special registers so it can
  2104.  * detect addressing modes that are silly for those registers.
  2105.  *
  2106.  * Where possible, it delivers 1 fatal or 1 warning message if the operand
  2107.  * is suspect. Exactly what we test for is still evolving.
  2108.  */
  2109.  
  2110. /*
  2111.  *               B u g s
  2112.  *
  2113.  *    Arg block.
  2114.  *
  2115.  * There were a number of 'mismatched argument type' bugs to vip_op.
  2116.  * The most general solution is to typedef each (of many) arguments.
  2117.  * We used instead a typedef'd argument block. This is less modular
  2118.  * than using seperate return pointers for each result, but runs faster
  2119.  * on most engines, and seems to keep programmers happy. It will have
  2120.  * to be done properly if we ever want to use vip_op as a general-purpose
  2121.  * module (it was designed to be).
  2122.  *
  2123.  *    G^
  2124.  *
  2125.  * Doesn't support DEC "G^" format operands. These always take 5 bytes
  2126.  * to express, and code as modes 8F or 9F. Reason: "G^" deprives you of
  2127.  * optimising to (say) a "B^" if you are lucky in the way you link.
  2128.  * When someone builds a linker smart enough to convert "G^" to "B^", "W^"
  2129.  * whenever possible, then we should implement it.
  2130.  * If there is some other use for "G^", feel free to code it in!
  2131.  *
  2132.  *
  2133.  *    speed
  2134.  *
  2135.  * If I nested if()s more, I could avoid testing (*err) which would save
  2136.  * time, space and page faults. I didn't nest all those if()s for clarity
  2137.  * and because I think the mode testing can be re-arranged 1st to test the
  2138.  * commoner constructs 1st. Does anybody have statistics on this?
  2139.  *
  2140.  *
  2141.  *
  2142.  *    error messages
  2143.  *
  2144.  * In future, we should be able to 'compose' error messages in a scratch area
  2145.  * and give the user MUCH more informative error messages. Although this takes
  2146.  * a little more code at run-time, it will make this module much more self-
  2147.  * documenting. As an example of what sucks now: most error messages have
  2148.  * hardwired into them the DEC VAX metacharacters "#^@" which are nothing like
  2149.  * the Un*x characters "$`*", that most users will expect from this AS.
  2150.  */
  2151.  
  2152. /*
  2153.  * The input is a string, ending with '\0'.
  2154.  *
  2155.  * We also require a 'hint' of what kind of operand is expected: so
  2156.  * we can remind caller not to write into literals for instance.
  2157.  *
  2158.  * The output is a skeletal instruction.
  2159.  *
  2160.  * The algorithm has two parts.
  2161.  * 1. extract the syntactic features (parse off all the @^#-()+[] mode crud);
  2162.  * 2. express the @^#-()+[] as some parameters suited to further analysis.
  2163.  *
  2164.  * 2nd step is where we detect the googles of possible invalid combinations
  2165.  * a human (or compiler) might write. Note that if we do a half-way
  2166.  * decent assembler, we don't know how long to make (eg) displacement
  2167.  * fields when we first meet them (because they may not have defined values).
  2168.  * So we must wait until we know how many bits are needed for each address,
  2169.  * then we can know both length and opcodes of instructions.
  2170.  * For reason(s) above, we will pass to our caller a 'broken' instruction
  2171.  * of these major components, from which our caller can generate instructions:
  2172.  *  -  displacement length      I^ S^ L^ B^ W^ unspecified
  2173.  *  -  mode                     (many)
  2174.  *  -  register                 R0-R15 or absent
  2175.  *  -  index register           R0-R15 or absent
  2176.  *  -  expression text          what we don't parse
  2177.  *  -  error text(s)            why we couldn't understand the operand
  2178.  */
  2179.  
  2180. /*
  2181.  * To decode output of this, test errtxt. If errtxt[0] == '\0', then
  2182.  * we had no errors that prevented parsing. Also, if we ever report
  2183.  * an internal bug, errtxt[0] is set non-zero. So one test tells you
  2184.  * if the other outputs are to be taken seriously.
  2185.  */
  2186.  
  2187.  
  2188.                 /* vax registers we need to know */
  2189. /* JF #define SP      (14)
  2190. /* JF for one big happy file #define PC      (15) */
  2191.  
  2192.                 /* useful ideas */
  2193. /* #define TRUE    (1) */
  2194. /* #define FALSE   (0) */
  2195.  
  2196. /*
  2197.  * Because this module is useful for both VMS and UN*X style assemblers
  2198.  * and because of the variety of UN*X assemblers we must recognise
  2199.  * the different conventions for assembler operand notation. For example
  2200.  * VMS says "#42" for immediate mode, while most UN*X say "$42".
  2201.  * We permit arbitrary sets of (single) characters to represent the
  2202.  * 3 concepts that DEC writes '#', '@', '^'.
  2203.  */
  2204.  
  2205.                 /* character tests */
  2206. #define IMMEDIATEP(c)    (vip_immediate_metacharacter [c])
  2207. #define INDIRECTP(c)    (vip_indirect_metacharacter  [c])
  2208. #define DISPLENP(c)    (vip_displen_metacharacter   [c])
  2209.  
  2210. /* We assume 8 bits per byte. Use vip_op_defaults() to set these up BEFORE we
  2211.  * are ever called.
  2212.  */
  2213.  
  2214. static char
  2215. vip_immediate_metacharacter [256], /* TRUE if character is like DEC # */
  2216. vip_indirect_metacharacter  [256], /* TRUE if character is like DEC @ */
  2217. vip_displen_metacharacter   [256]; /* TRUE if character is like DEC ^ */
  2218.  
  2219. static vip_op_1();
  2220.  
  2221. vip_op_defaults(immediate, indirect, displen) /* can be called any time */
  2222.      char *    immediate,    /* Strings of characters for each job. */
  2223.            *    indirect,
  2224.       *    displen;    /* more arguments may appear in future! */
  2225. {
  2226.   vip_op_1 (vip_immediate_metacharacter, immediate);
  2227.   vip_op_1 (vip_indirect_metacharacter,  indirect );
  2228.   vip_op_1 (vip_displen_metacharacter,   displen  );
  2229. }
  2230.  
  2231. static
  2232. vip_op_1 (table, truths)
  2233.      char   table[256];
  2234.      char * truths;
  2235. {
  2236.   bzero (table, sizeof(table));
  2237.   for (; *truths; truths++)
  2238.     {
  2239.       table [*truths] = TRUE;
  2240.     }
  2241. }
  2242.  
  2243. /*
  2244.  * Dec defines the semantics of address modes (and values)
  2245.  * by a two-letter code, explained here.
  2246.  *
  2247.  *   letter 1:   access type
  2248.  *
  2249.  *     a         address calculation - no data access, registers forbidden
  2250.  *     b         branch displacement
  2251.  *     m         read - let go of bus - write back    "modify"
  2252.  *     r         read
  2253.  *     v         bit field address: like 'a' but registers are OK
  2254.  *     w         write
  2255.  *     space     no operator (eg ".long foo") [our convention]
  2256.  *
  2257.  *   letter 2:   data type (i.e. width, alignment)
  2258.  *
  2259.  *     b         byte
  2260.  *     d         double precision floating point (D format)
  2261.  *     f         single precision floating point (F format)
  2262.  *     g         G format floating
  2263.  *     h         H format floating
  2264.  *     l         longword
  2265.  *     o         octaword
  2266.  *     q         quadword
  2267.  *     w         word
  2268.  *     ?     simple synthetic branch operand
  2269.  *     -     unconditional synthetic JSB/JSR operand
  2270.  *     !     complex synthetic branch operand
  2271.  *
  2272.  * The '-?!' letter 2's are not for external consumption. They are used
  2273.  * for various assemblers. Generally, all unknown widths are assumed 0.
  2274.  * We don't limit your choice of width character.
  2275.  *
  2276.  * DEC operands are hard work to parse. For example, '@' as the first
  2277.  * character means indirect (deferred) mode but elswhere it is a shift
  2278.  * operator.
  2279.  * The long-winded explanation of how this is supposed to work is
  2280.  * cancelled. Read a DEC vax manual.
  2281.  * We try hard not to parse anything that MIGHT be part of the expression
  2282.  * buried in that syntax. For example if we see @...(Rn) we don't check
  2283.  * for '-' before the '(' because mode @-(Rn) does not exist.
  2284.  *
  2285.  * After parsing we have:
  2286.  *
  2287.  * at                     TRUE if leading '@' (or Un*x '*')
  2288.  * len                    takes one value from " bilsw". eg B^ -> 'b'.
  2289.  * hash                   TRUE if leading '#' (or Un*x '$')
  2290.  * expr_begin, expr_end   the expression we did not parse
  2291.  *                        even though we don't interpret it, we make use
  2292.  *                        of its presence or absence.
  2293.  * sign                   -1: -(Rn)    0: absent    +1: (Rn)+
  2294.  * paren                  TRUE if () are around register
  2295.  * reg                    major register number 0:15    -1 means absent
  2296.  * ndx                    index register number 0:15    -1 means absent
  2297.  *
  2298.  * Again, I dare not explain it: just trace ALL the code!
  2299.  */
  2300.  
  2301. char *                /* (code here) bug message, "" = OK */
  2302.                 /* our code bug, NOT bad assembly language */
  2303. vip_op (optext, vopP)
  2304.      char *        optext;    /* user's input string e.g.: */
  2305.                 /* "@B^foo@bar(AP)[FP]:" */
  2306.      struct vop *  vopP;    /* In: vop_access, vop_width. */
  2307.                 /* Out: _ndx, _reg, _mode, _short, _warn, */
  2308.                 /* _error _expr_begin, _expr_end, _nbytes. */
  2309.                 /* vop_nbytes : number of bytes in a datum. */
  2310. {
  2311.   char *              p;    /* track operand text forward */
  2312.   char *              q;    /* track operand text backward */
  2313.   int                at;    /* TRUE if leading '@'('*') seen */
  2314.   char              len;    /* one of " bilsw" */
  2315.   int              hash;    /* TRUE if leading '#'('$') seen */
  2316.   int              sign;    /* -1, 0 or +1 */
  2317.   int              paren;    /* TRUE if () surround register */
  2318.   int               reg;    /* register number, -1:absent */
  2319.   int               ndx;    /* index register number -1:absent */
  2320.   char *            bug;    /* report any logic error in here, ""==OK */
  2321.   char *            err;    /* report illegal operand, ""==OK */
  2322.                 /* " " is a FAKE error: means we won */
  2323.                 /* ANY err that begins with ' ' is a fake. */
  2324.                 /* " " is converted to "" before return */
  2325.   char *            wrn;    /* warn about weird modes pf address */
  2326.   char *           oldq;    /* preserve q in case we backup */
  2327.   int              mode;    /* build up 4-bit operand mode here */
  2328.                 /* note: index mode is in ndx, this is */
  2329.                 /* the major mode of operand address */
  2330. /*
  2331.  * Notice how we move wrong-arg-type bugs INSIDE this module: if we
  2332.  * get the types wrong below, we lose at compile time rather than at
  2333.  * lint or run time.
  2334.  */
  2335.   char        access;        /* vop_access. */
  2336.   char        width;        /* vop_width. */
  2337.  
  2338.   int   vax_reg_parse();    /* returns 0:15 or -1 if not a register */
  2339.  
  2340.   access = vopP -> vop_access;
  2341.   width = vopP -> vop_width;
  2342.   bug =                /* none of our code bugs (yet) */
  2343.   err =             /* no user text errors */
  2344.   wrn = "";            /* no warnings even */
  2345.  
  2346.   p = optext;
  2347.  
  2348.   if (*p == ' ')        /* Expect all whitespace reduced to ' '. */
  2349.       p++;            /* skip over whitespace */
  2350.  
  2351.   if ( at = INDIRECTP(*p) ) {    /* TRUE if *p=='@'(or '*' for Un*x) */
  2352.       p++;            /* at is determined */
  2353.       if (*p == ' ')        /* Expect all whitespace reduced to ' '. */
  2354.     p++;            /* skip over whitespace */
  2355.   }
  2356.  
  2357.   /*
  2358.    * This code is subtle. It tries to detect all legal (letter)'^'
  2359.    * but it doesn't waste time explicitly testing for premature '\0' because
  2360.    * this case is rejected as a mismatch against either (letter) or '^'.
  2361.    */
  2362.   {
  2363.     register char    c;
  2364.  
  2365.     c = *p;
  2366.     if (isupper(c)) c = tolower(c);
  2367.     if ( DISPLENP(p[1]) && index("bilws", len = c) )
  2368.     p += 2;            /* skip (letter) '^' */
  2369.     else            /* no (letter) '^' seen */
  2370.     len = ' ';        /* len is determined */
  2371.   }
  2372.  
  2373.   if (*p == ' ')        /* Expect all whitespace reduced to ' '. */
  2374.       p++;        /* skip over whitespace */
  2375.  
  2376.   if ( hash = IMMEDIATEP(*p) )    /* TRUE if *p=='#' ('$' for Un*x) */
  2377.       p++;            /* hash is determined */
  2378.  
  2379.   /*
  2380.    * p points to what may be the beginning of an expression.
  2381.    * We have peeled off the front all that is peelable.
  2382.    * We know at, len, hash.
  2383.    *
  2384.    * Lets point q at the end of the text and parse that (backwards).
  2385.    */
  2386.  
  2387.   for (q=p; *q; q++)
  2388.     ;
  2389.   q--;                /* now q points at last char of text */
  2390.  
  2391.   if ( *q == ' ' && q >= p )    /* Expect all whitespace reduced to ' '. */
  2392.       q--;
  2393.                 /* reverse over whitespace, but don't */
  2394.                 /* run back over *p */
  2395.  
  2396.   /*
  2397.    * As a matter of policy here, we look for [Rn], although both Rn and S^#
  2398.    * forbid [Rn]. This is because it is easy, and because only a sick
  2399.    * cyborg would have [...] trailing an expression in a VAX-like assembler.
  2400.    * A meticulous parser would first check for Rn followed by '(' or '['
  2401.    * and not parse a trailing ']' if it found another. We just ban expressions
  2402.    * ending in ']'.
  2403.    */
  2404.   if (*q==']')
  2405.     {
  2406.       while (q>=p && *q!='[')
  2407.       q--;
  2408.                 /* either q<p or we got matching '[' */
  2409.       if (q<p)
  2410.       err = "no '[' to match ']'";
  2411.       else
  2412.     {
  2413.       /*
  2414.        * Confusers like "[]" will eventually lose with a bad register
  2415.        * name error. So again we don't need to check for early '\0'.
  2416.        */
  2417.       if (q[3] == ']')
  2418.           ndx = vax_reg_parse(q[1],q[2],0);
  2419.       else if (q[4] == ']')
  2420.           ndx = vax_reg_parse(q[1],q[2],q[3]);
  2421.       else
  2422.           ndx = -1;
  2423.       /*
  2424.        * Since we saw a ']' we will demand a register name in the [].
  2425.        * If luser hasn't given us one: be rude.
  2426.        */
  2427.       if (ndx < 0)
  2428.           err = "bad register in []";
  2429.       else if (ndx == PC)
  2430.           err = "[PC] index banned";
  2431.       else
  2432.           q--;        /* point q just before "[...]" */
  2433.     }
  2434.     }
  2435.   else
  2436.       ndx = -1;            /* no ']', so no iNDeX register */
  2437.  
  2438.   /*
  2439.    * If err = "..." then we lost: run away.
  2440.    * Otherwise ndx == -1 if there was no "[...]".
  2441.    * Otherwise, ndx is index register number, and q points before "[...]".
  2442.    */
  2443.  
  2444.   if ( *q == ' ' && q >= p )    /* Expect all whitespace reduced to ' '. */
  2445.       q--;
  2446.                 /* reverse over whitespace, but don't */
  2447.                 /* run back over *p */
  2448.   if (!*err)
  2449.     {
  2450.       sign = 0;            /* no ()+ or -() seen yet */
  2451.  
  2452.       if ( q>p+3 && *q=='+' && q[-1]==')' )
  2453.     {
  2454.       sign = 1;        /* we saw a ")+" */
  2455.       q--;            /* q points to ')' */
  2456.     }
  2457.  
  2458.       if ( *q==')' && q>p+2 )
  2459.     {
  2460.       paren = TRUE;        /* assume we have "(...)" */
  2461.       while ( q>=p && *q!='(' )
  2462.           q--;
  2463.                 /* either q<p or we got matching '(' */
  2464.       if (q<p)
  2465.           err = "no '(' to match ')'";
  2466.       else
  2467.         {
  2468.           /*
  2469.            * Confusers like "()" will eventually lose with a bad register
  2470.            * name error. So again we don't need to check for early '\0'.
  2471.            */
  2472.           if ( q[3] == ')' )
  2473.           reg = vax_reg_parse(q[1],q[2],0);
  2474.           else if ( q[4] == ')' )
  2475.           reg = vax_reg_parse(q[1],q[2],q[3]);
  2476.           else
  2477.           reg = -1;
  2478.           /*
  2479.            * Since we saw a ')' we will demand a register name in the ')'.
  2480.            * This is nasty: why can't our hypothetical assembler permit
  2481.            * parenthesised expressions? BECAUSE I AM LAZY! That is why.
  2482.            * Abuse luser if we didn't spy a register name.
  2483.            */
  2484.           if ( reg < 0 ) {
  2485.               /* JF allow parenthasized expressions.  I hope this works */
  2486.               paren = FALSE;
  2487.           while(*q!=')')
  2488.               q++;
  2489.           /* err = "unknown register in ()"; */
  2490.           }
  2491.           else
  2492.           q--;        /* point just before '(' of "(...)" */
  2493.           /*
  2494.            * If err == "..." then we lost. Run away.
  2495.            * Otherwise if reg >= 0 then we saw (Rn).
  2496.            */
  2497.         }
  2498.       /*
  2499.        * If err == "..." then we lost.
  2500.        * Otherwise paren==TRUE and reg = register in "()".
  2501.        */
  2502.     }
  2503.       else
  2504.       paren = FALSE;
  2505.       /*
  2506.        * If err == "..." then we lost.
  2507.        * Otherwise, q points just before "(Rn)", if any.
  2508.        * If there was a "(...)" then paren==TRUE, and reg is the register.
  2509.        */
  2510.  
  2511.       /*
  2512.        * We should only seek '-' of "-(...)" if:
  2513.        *   we saw "(...)"                    paren == TRUE
  2514.        *   we have no errors so far          ! *err
  2515.        *   we did not see '+' of "(...)+"    sign < 1
  2516.        * We don't check len. We want a specific error message later if
  2517.        * user tries "x^...-(Rn)". This is a feature not a bug.
  2518.        */
  2519.       if ( !*err )
  2520.     {
  2521.       if ( paren && sign<1 ) /* !sign is adequate test */
  2522.         {
  2523.           if ( *q == '-' )
  2524.         {
  2525.           sign = -1;
  2526.           q--;
  2527.         }
  2528.         }
  2529.       /*
  2530.        * We have back-tracked over most
  2531.        * of the crud at the end of an operand.
  2532.        * Unless err, we know: sign, paren. If paren, we know reg.
  2533.        * The last case is of an expression "Rn".
  2534.        * This is worth hunting for if !err, !paren.
  2535.        * We wouldn't be here if err.
  2536.        * We remember to save q, in case we didn't want "Rn" anyway.
  2537.        */
  2538.       if ( !paren )
  2539.         {
  2540.           if (*q == ' ' && q >= p) /* Expect all whitespace reduced to ' '. */
  2541.           q--;
  2542.                 /* reverse over whitespace, but don't */
  2543.                 /* run back over *p */
  2544.           if ( q>p && q<p+3 ) /* room for Rn or Rnn exactly? */
  2545.           reg = vax_reg_parse(p[0],p[1],q<p+2?0:p[2]);
  2546.           else
  2547.           reg = -1;    /* always comes here if no register at all */
  2548.           /*
  2549.            * Here with a definitive reg value.
  2550.            */
  2551.           if ( reg >= 0 )
  2552.         {
  2553.           oldq = q;
  2554.           q = p-1;
  2555.         }
  2556.         }
  2557.     }
  2558.     }
  2559.   /*
  2560.    * have reg. -1:absent; else 0:15
  2561.    */
  2562.  
  2563.   /*
  2564.    * We have:  err, at, len, hash, ndx, sign, paren, reg.
  2565.    * Also, any remaining expression is from *p through *q inclusive.
  2566.    * Should there be no expression, q==p-1. So expression length = q-p+1.
  2567.    * This completes the first part: parsing the operand text.
  2568.    */
  2569.  
  2570.   /*
  2571.    * We now want to boil the data down, checking consistency on the way.
  2572.    * We want:  len, mode, reg, ndx, err, p, q, wrn, bug.
  2573.    * We will deliver a 4-bit reg, and a 4-bit mode.
  2574.    */
  2575.  
  2576.   /*
  2577.    * Case of branch operand. Different. No L^B^W^I^S^ allowed for instance.
  2578.    *
  2579.    * in:  at    ?
  2580.    *      len    ?
  2581.    *      hash    ?
  2582.    *      p:q    ?
  2583.    *      sign  ?
  2584.    *      paren    ?
  2585.    *      reg   ?
  2586.    *      ndx   ?
  2587.    *
  2588.    * out: mode  0
  2589.    *      reg   -1
  2590.    *      len    ' '
  2591.    *      p:q    whatever was input
  2592.    *      ndx    -1
  2593.    *      err    " "         or error message, and other outputs trashed
  2594.    */
  2595.   if ( ! * err )
  2596.     {
  2597.       if ( access == 'b' )
  2598.     {            /* branch operands have restricted forms */
  2599.       if(at || hash || sign || paren || ndx >= 0 || reg >= 0 || len != ' ')
  2600.         err = "invalid branch operand";
  2601.       else
  2602.         err = " ";
  2603.     }
  2604.     }
  2605.  
  2606. /* Since nobody seems to use it: comment this 'feature'(?) out for now. */
  2607. #ifdef NEVER
  2608.   /*
  2609.    * Case of stand-alone operand. e.g. ".long foo"
  2610.    *
  2611.    * in:  at    ?
  2612.    *      len    ?
  2613.    *      hash    ?
  2614.    *      p:q    ?
  2615.    *      sign  ?
  2616.    *      paren    ?
  2617.    *      reg   ?
  2618.    *      ndx   ?
  2619.    *
  2620.    * out: mode  0
  2621.    *      reg   -1
  2622.    *      len    ' '
  2623.    *      p:q    whatever was input
  2624.    *      ndx    -1
  2625.    *      err    " "         or error message, and other outputs trashed
  2626.    */
  2627.   if ( ! * err )
  2628.     {
  2629.       if ( access == ' ' )
  2630.     {            /* addresses have restricted forms */
  2631.       if ( at )
  2632.           err = "address prohibits @";
  2633.       else
  2634.         {
  2635.           if ( hash )
  2636.           err = "address prohibits #";
  2637.           else
  2638.         {
  2639.           if ( sign )
  2640.             {
  2641.               if ( sign < 0 )
  2642.               err = "address prohibits -()";
  2643.               else
  2644.               err = "address prohibits ()+";
  2645.             }
  2646.           else
  2647.             {
  2648.               if ( paren )
  2649.               err = "address prohibits ()";
  2650.               else
  2651.             {
  2652.               if ( ndx >= 0 )
  2653.                   err = "address prohibits []";
  2654.               else
  2655.                 {
  2656.                   if ( reg >= 0 )
  2657.                   err = "address prohibits register";
  2658.                   else
  2659.                 {
  2660.                   if ( len != ' ' )
  2661.                       err = "address prohibits displacement length specifier";
  2662.                   else
  2663.                     {
  2664.                       err = " "; /* succeed */
  2665.                       mode = 0;
  2666.                     }
  2667.                 }
  2668.                 }
  2669.             }
  2670.             }
  2671.         }
  2672.         }
  2673.     }
  2674.     }
  2675. #endif /*#Ifdef NEVER*/
  2676.  
  2677.   /*
  2678.    * Case of S^#.
  2679.    *
  2680.    * in:  at       FALSE
  2681.    *      len      's'               definition
  2682.    *      hash     TRUE              demand
  2683.    *      p:q                        demand not empty
  2684.    *      sign     0                 by paren==FALSE
  2685.    *      paren    FALSE             by "()" scan logic because "S^" seen
  2686.    *      reg      -1                or nn by mistake
  2687.    *      ndx      -1
  2688.    *
  2689.    * out: mode     0
  2690.    *      reg      -1
  2691.    *      len      's'
  2692.    *      exp
  2693.    *      ndx      -1
  2694.    */
  2695.   if ( ! * err )
  2696.     {
  2697.       if ( len == 's' )
  2698.     {
  2699.       if ( ! hash || paren || at || ndx >= 0)
  2700.         err = "invalid operand of S^#";
  2701.       else {
  2702.           if (reg >= 0)
  2703.         {
  2704.                   /*
  2705.                    * SHIT! we saw S^#Rnn ! put the Rnn back in
  2706.                    * expression. KLUDGE! Use oldq so we don't
  2707.                    * need to know exact length of reg name.
  2708.                    */
  2709.               q = oldq;
  2710.               reg = 0;
  2711.          }
  2712.               /*
  2713.                * We have all the expression we will ever get.
  2714.                */
  2715.         if (p>q)
  2716.               err = "S^# needs expression";
  2717.         else if ( access == 'r' )
  2718.           {
  2719.             err = " "; /* WIN! */
  2720.             mode = 0;
  2721.           }
  2722.         else
  2723.           err = "S^# may only read-access";
  2724.     }
  2725.       }
  2726.     }
  2727.  
  2728.   /*
  2729.    * Case of -(Rn), which is weird case.
  2730.    *
  2731.    * in:  at       FALSE
  2732.    *      len      '
  2733.    *      hash     FALSE
  2734.    *      p:q      q<p
  2735.    *      sign     -1                by definition
  2736.    *      paren    TRUE              by definition
  2737.    *      reg      present           by definition
  2738.    *      ndx      optional
  2739.    *
  2740.    * out: mode     7
  2741.    *      reg      present
  2742.    *      len      ' '
  2743.    *      exp      ""                enforce empty expression
  2744.    *      ndx      optional          warn if same as reg
  2745.    */
  2746.   if ( ! * err )
  2747.     {
  2748.       if ( sign<0 )
  2749.     {
  2750.       if ( len != ' ' || hash || at || p<=q)
  2751.           err = "invalid operand of -()";
  2752.       else
  2753.         {
  2754.           err = " "; /* win */
  2755.           mode = 7;
  2756.           if ( reg == PC )
  2757.               wrn = "-(PC) unprdeictable";
  2758.           else if ( reg == ndx )
  2759.               wrn = "[]index same as -()register: unpredictable";
  2760.         }
  2761.     }
  2762.     }
  2763.  
  2764.   /*
  2765.    * We convert "(Rn)" to "@Rn" for our convenience.
  2766.    * (I hope this is convenient: has someone got a better way to parse this?)
  2767.    * A side-effect of this is that "@Rn" is a valid operand.
  2768.    */
  2769.   if ( paren && !sign && !hash && !at && len==' ' && p>q )
  2770.     {
  2771.       at    = TRUE;
  2772.       paren = FALSE;
  2773.     }
  2774.   
  2775.   /*
  2776.    * Case of (Rn)+, which is slightly different.
  2777.    *
  2778.    * in:  at       
  2779.    *      len      ' '
  2780.    *      hash     FALSE
  2781.    *      p:q      q<p
  2782.    *      sign     +1                by definition
  2783.    *      paren    TRUE              by definition
  2784.    *      reg      present           by definition
  2785.    *      ndx      optional
  2786.    *
  2787.    * out: mode     8+@
  2788.    *      reg      present
  2789.    *      len      ' '
  2790.    *      exp      ""                enforce empty expression
  2791.    *      ndx      optional          warn if same as reg
  2792.    */
  2793.   if ( ! * err )
  2794.     {
  2795.       if ( sign > 0 )
  2796.     {
  2797.       if ( len != ' ' || hash || p<=q)
  2798.           err = "invalid operand of ()+";
  2799.       else
  2800.         {
  2801.           err = " "; /* win */
  2802.           mode = 8 + (at ? 1 : 0);
  2803.           if (reg == PC )
  2804.           wrn = "(PC)+ unpredictable";
  2805.           else if (reg == ndx)
  2806.           wrn = "[]index same as ()+register: unpredictable";
  2807.         }
  2808.     }
  2809.     }
  2810.  
  2811.   /*
  2812.    * Case of #, without S^.
  2813.    *
  2814.    * in:  at       
  2815.    *      len      ' ' or 'i'
  2816.    *      hash     TRUE              by definition
  2817.    *      p:q
  2818.    *      sign     0
  2819.    *      paren    FALSE
  2820.    *      reg      absent
  2821.    *      ndx      optional
  2822.    *
  2823.    * out: mode     8+@
  2824.    *      reg      PC
  2825.    *      len      ' ' or 'i'
  2826.    *      exp
  2827.    *      ndx      optional
  2828.    */
  2829.   if (! * err)
  2830.     {
  2831.       if (hash)
  2832.     {
  2833.       if (len!='i' && len!=' ')
  2834.           err = "# conflicts length";
  2835.       else if (paren)
  2836.           err = "# bars register";
  2837.       else
  2838.         {
  2839.           if (reg >= 0)
  2840.         {
  2841.               /*
  2842.                * SHIT! we saw #Rnn! Put the Rnn back into the expression.
  2843.                * By using oldq, we don't need to know how long Rnn was.
  2844.                * KLUDGE!
  2845.                */
  2846.               q = oldq;
  2847.               reg = -1;    /* no register any more */
  2848.         }
  2849.           err = " ";    /* win */
  2850.           mode = (at ? 9 : 8);
  2851.           reg = PC;
  2852.           if ( (access=='m' || access=='w') && !at)
  2853.         wrn = "writing or modifying # is unpredictable";
  2854.         }
  2855.     }
  2856.     }
  2857.   /*
  2858.    * If !*err, then        sign == 0
  2859.    *                       hash == FALSE
  2860.    */
  2861.   
  2862.   /*
  2863.    * Case of Rn. We seperate this one because it has a few special
  2864.    * errors the remaining modes lack.
  2865.    *
  2866.    * in:  at       optional
  2867.    *      len      ' '
  2868.    *      hash     FALSE             by program logic
  2869.    *      p:q      empty
  2870.    *      sign     0                 by program logic
  2871.    *      paren    FALSE             by definition
  2872.    *      reg      present           by definition
  2873.    *      ndx      optional
  2874.    *
  2875.    * out: mode     5+@
  2876.    *      reg      present
  2877.    *      len      ' '               enforce no length
  2878.    *      exp      ""                enforce empty expression
  2879.    *      ndx      optional          warn if same as reg
  2880.    */
  2881.     if (! * err && !paren && reg>=0)
  2882.       {
  2883.     if (len!=' ')
  2884.       err = "length not needed";
  2885.     else if (at)
  2886.       {
  2887.          err  = " "; /* win */
  2888.          mode = 6;    /* @Rn */
  2889.       }
  2890.     else if (ndx>=0)
  2891.       err = "can't []index a register, because it has no address";
  2892.     else if (access=='a')
  2893.       err = "a register has no address";
  2894.     else
  2895.       {
  2896.       /*
  2897.       * Idea here is to detect from length of datum
  2898.       * and from register number if we will touch PC.
  2899.       * Warn if we do.
  2900.       * vop_nbytes is number of bytes in operand.
  2901.       * Compute highest byte affected, compare to PC0.
  2902.       */
  2903.         if ( (vopP->vop_nbytes + reg*4) > 60)
  2904.           wrn = "PC part of operand unpredictable";
  2905.         err  = " ";    /* win */
  2906.         mode = 5; /* Rn */
  2907.       }
  2908.       }
  2909.   /*
  2910.    * If !*err,        sign  == 0
  2911.    *                  hash  == FALSE
  2912.    *                  paren == TRUE  OR reg==-1
  2913.    */
  2914.   
  2915.   /*
  2916.    * Rest of cases fit into one bunch.
  2917.    *
  2918.    * in:  at       optional
  2919.    *      len      ' ' or 'b' or 'w' or 'l'
  2920.    *      hash     FALSE             by program logic
  2921.    *      p:q      expected          (empty is not an error)
  2922.    *      sign     0                 by program logic
  2923.    *      paren    optional
  2924.    *      reg      optional
  2925.    *      ndx      optional
  2926.    *
  2927.    * out: mode     10 + @ + len
  2928.    *      reg      optional
  2929.    *      len      ' ' or 'b' or 'w' or 'l'
  2930.    *      exp                        maybe empty
  2931.    *      ndx      optional          warn if same as reg
  2932.    */
  2933.     if (!*err)
  2934.       {
  2935.     err = " ";        /* win (always) */
  2936.     mode = 10 + (at ? 1 : 0);
  2937.     switch (len)
  2938.       {
  2939.       case 'l':
  2940.         mode += 2;
  2941.       case 'w':
  2942.         mode += 2;
  2943.       case ' ':        /* assumed B^ until our caller changes it */
  2944.       case 'b':
  2945.         break;
  2946.       }
  2947.       }
  2948.  
  2949.   /*
  2950.    * here with completely specified     mode
  2951.    *                    len
  2952.    *                    reg
  2953.    *                    expression   p,q
  2954.    *                    ndx
  2955.    */
  2956.  
  2957.   if (*err==' ')
  2958.       err = "";            /* " " is no longer an error */
  2959.  
  2960.   vopP -> vop_mode        = mode;
  2961.   vopP -> vop_reg        = reg;
  2962.   vopP -> vop_short        = len;
  2963.   vopP -> vop_expr_begin    = p;
  2964.   vopP -> vop_expr_end        = q;
  2965.   vopP -> vop_ndx        = ndx;
  2966.   vopP -> vop_error        = err;
  2967.   vopP -> vop_warn        = wrn;
  2968.   return(bug);
  2969.  
  2970. }                /* vip_op() */
  2971.  
  2972. /*
  2973.  
  2974. Summary of vip_op outputs.
  2975.  
  2976.             mode    reg    len    ndx
  2977. (Rn) => @Rn
  2978. {@}Rn            5+@    n    ' '    optional
  2979. branch operand        0    -1    ' '    -1
  2980. S^#foo            0    -1    's'    -1
  2981. -(Rn)            7    n    ' '    optional
  2982. {@}(Rn)+        8+@    n    ' '    optional
  2983. {@}#foo, no S^        8+@    PC    " i"    optional
  2984. {@}{q^}{(Rn)}        10+@+q    option    " bwl"    optional
  2985.  
  2986. */
  2987.  
  2988. #ifdef TEST            /* #Define to use this testbed. */
  2989.  
  2990. /*
  2991.  * Follows a test program for this function.
  2992.  * We declare arrays non-local in case some of our tiny-minded machines
  2993.  * default to small stacks. Also, helps with some debuggers.
  2994.  */
  2995.  
  2996. #include <stdio.h>
  2997.  
  2998. char answer[100];        /* human types into here */
  2999. char * p;            /*  */
  3000. char * myerr;
  3001. char * mywrn;
  3002. char * mybug;
  3003. char   myaccess;
  3004. char   mywidth;
  3005. char   mymode;
  3006. char   myreg;
  3007. char   mylen;
  3008. char * myleft;
  3009. char * myright;
  3010. char   myndx;
  3011. int    my_operand_length;
  3012. char    my_immediate[200];
  3013. char    my_indirect[200];
  3014. char    my_displen[200];
  3015.  
  3016. main()
  3017. {
  3018.   char * vip_op();        /* make cc happy */
  3019.  
  3020.   printf("enter immediate symbols eg enter #   ");
  3021.   gets(my_immediate);
  3022.   printf("enter indirect symbols  eg enter @   ");
  3023.   gets(my_indirect);
  3024.   printf("enter displen symbols   eg enter ^   ");
  3025.   gets(my_displen);
  3026.   vip_op_defaults (my_immediate, my_indirect, my_displen);
  3027.   for (;;)
  3028.     {
  3029.       printf("access,width (eg 'ab' or 'wh') [empty line to quit] :  ");
  3030.       fflush(stdout);
  3031.       gets(answer);
  3032.       if (!answer[0])
  3033.       exit(0);
  3034.       myaccess = answer[0];
  3035.       mywidth  = answer[1];
  3036.       switch ( mywidth )
  3037.     {
  3038.     case 'b':    my_operand_length = 1;    break;
  3039.     case 'd':    my_operand_length = 8;    break;
  3040.     case 'f':    my_operand_length = 4;    break;
  3041.     case 'g':    my_operand_length = 16;    break;
  3042.     case 'h':    my_operand_length = 32;    break;
  3043.     case 'l':    my_operand_length = 4;    break;
  3044.     case 'o':    my_operand_length = 16;    break;
  3045.     case 'q':    my_operand_length = 8;    break;
  3046.     case 'w':    my_operand_length = 2;    break;
  3047.     case '!':
  3048.     case '?':
  3049.     case '-':    my_operand_length = 0;    break;
  3050.  
  3051.               default:
  3052.             my_operand_length = 2;
  3053.             printf("I dn't understand access width %c\n",mywidth);
  3054.             break;
  3055.     }
  3056.       printf("VAX assembler instruction operand: ");
  3057.       fflush(stdout);
  3058.       gets(answer);
  3059.       mybug = vip_op( answer, myaccess, mywidth, my_operand_length,
  3060.              &mymode, &myreg, &mylen, &myleft, &myright, &myndx,
  3061.              &myerr, &mywrn);
  3062.       if (*myerr)
  3063.     {
  3064.       printf("error: \"%s\"\n",myerr);
  3065.       if (*mybug)
  3066.           printf(" bug: \"%s\"\n",mybug);
  3067.     }
  3068.       else
  3069.     {
  3070.       if (*mywrn)
  3071.           printf("warning: \"%s\"\n",mywrn);
  3072.       mumble("mode"    ,mymode);
  3073.       mumble("register",myreg);
  3074.       mumble("index"   ,myndx);
  3075.       printf("width:'%c'  ",mylen);
  3076.       printf("expression: \"");
  3077.       while (myleft<=myright)
  3078.           putchar(*myleft++);
  3079.       printf("\"\n");
  3080.     }
  3081.     }
  3082. }
  3083.  
  3084. mumble(text,value)
  3085.      char * text;
  3086.      int    value;
  3087. {
  3088.   printf("%s:",text);
  3089.   if (value>=0)
  3090.       printf("%xx",value);
  3091.   else
  3092.       printf("ABSENT");
  3093.   printf("  ");
  3094. }
  3095.  
  3096. #endif                /* ifdef TEST */
  3097.  
  3098. /* end: vip_op.c */
  3099.  
  3100. /* end: vax.c */
  3101.  
  3102. /* JF: new routines */
  3103.  
  3104. int md_short_jump_size=3;
  3105.  
  3106. int md_long_jump_size=6;
  3107.  
  3108. void
  3109. md_create_short_jump(ptr,from_addr,to_addr,frag,to_symbol)
  3110. char *ptr;
  3111. long    from_addr,
  3112.     to_addr;
  3113. fragS    *frag;
  3114. symbolS *to_symbol;
  3115. {
  3116.     long offset;
  3117.  
  3118.     offset=to_addr - (from_addr+1);
  3119.     *ptr++= 0x31;
  3120.     md_number_to_chars(ptr,offset,2);
  3121. }
  3122.  
  3123. void
  3124. md_create_long_jump(ptr,from_addr,to_addr,frag,to_symbol)
  3125. char    *ptr;
  3126. long    from_addr,
  3127.     to_addr;
  3128. fragS    *frag;
  3129. symbolS    *to_symbol;
  3130. {
  3131.     long offset;
  3132.  
  3133.     offset=to_addr-to_symbol->sy_value;
  3134.     *ptr++=0x17;
  3135.     *ptr++=0x9F;
  3136.     md_number_to_chars(ptr,offset,4);
  3137.     fix_new(frag,ptr-frag->fr_literal,4,to_symbol,(symbolS *)0,(long int)0,0);
  3138. }
  3139.  
  3140. int
  3141. md_parse_option(argP,cntP,vecP)
  3142. char **argP;
  3143. int *cntP;
  3144. char ***vecP;
  3145. {
  3146.     char    *temp_file_name;    /* file name for -t option */
  3147.  
  3148.     switch(**argP) {
  3149.     case 'd':
  3150.         as_warn("Displacement length %s. ignored! GNU is NOT Un*x!",*argP);
  3151.         break;
  3152.  
  3153.     case 'J':
  3154.         as_warn("I can do better than -J!");
  3155.         break;
  3156.  
  3157.     case 'S':
  3158.         as_warn("SYMBOL TABLE not implemented");
  3159.         break;    /* SYMBOL TABLE not implemented */
  3160.  
  3161.     case 'T':
  3162.         as_warn("TOKEN TRACE not implemented");
  3163.         break;    /* TOKEN TRACE not implemented */
  3164.  
  3165.     case 't':
  3166.         if (**argP) {    /* Rest of argument is filename. */
  3167.             temp_file_name = *argP;
  3168.             while(**argP)
  3169.                 (*argP)++;
  3170.         } else if (*cntP) {
  3171.             while(**argP)
  3172.                 (*argP)++;
  3173.             --(*cntP);
  3174.             temp_file_name = *++(*vecP);
  3175.             **vecP = NULL; /* Remember this is not a file-name. */
  3176.         } else {
  3177.             as_warn("I expected a filename after -t.");
  3178.             temp_file_name = "{absent}";
  3179.         }
  3180.         as_warn("I don't need or use temp. file \"%s\".",temp_file_name);
  3181.         break;
  3182.  
  3183.     case 'V':
  3184.         as_warn("I don't use an interpass file! -V ignored");
  3185.         break;
  3186.  
  3187.     default:
  3188.         return 0;
  3189.  
  3190.     }
  3191.     return 1;
  3192. }
  3193.